线程和进程的区别及Python代码实例
在程序猿的世界中,线程和进程是一个很重要的概念,很多人经常弄不清线程和进程到底是什么,有什么区别,本文试图来解释一下线程和进程。首先来看一下概念:
进程(英语:process),是计算机中已运行程序的实体。进程为曾经是分时系统的基本运作单位。在面向进程设计的系统(如早期的UNIX,Linux 2.4及更早的版本)中,进程是程序的基本执行实体;在面向线程设计的系统(如当代多数操作系统、Linux 2.6及更新的版本)中,进程本身不是基本运行单位,而是线程的容器。程序本身只是指令、数据及其组织形式的描述,进程才是程序(那些指令和数据)的真正运行实例。–维基百科
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。–维基百科
概念太吓人了,先来看一下进程,这个相对于线程来说还是稍微好理解一点的。进程,是程序运行的实体,这句话的意思是,程序是存放在硬盘中的,当这个程序运行时,就会产生若干个进程,并且这个进程是可见的,在windows8的任务管理器中,我们看到有下列进程:
那么什么是线程呢?如果你编写过程序,特别是类似windows的批处理这样的逻辑控制语句少的语言,就能明显感觉到,当运行一个程序是,实际上整个运行的过程是从头部一直运行到尾部。例如有这样一段批处理的代码,提示用户输入姓名,然后根据姓名输出问候:
@echo off set /p name=请输入您的姓名: cls echo 你好,%name pause
这段小程序就是自上而下执行,执行完毕则退出。不管是批处理,像PHP,Node.JS等都是这样自上而下执行的。实际上这就是一个线程,可以这样去理解:线程是一个任务流,它被包含在进程之中。有个例子:
5月1号这一天,麦当劳生意比较火爆,人很多,前台有6个窗口,有4个窗口在工作,随着要吃饭的人变多,麦当劳不得不开放了剩余的两个窗口。在这里,每一个窗口就是一个进程,处理卖垃圾食品这样一个任务,让系统需要处理更多请求时候,开放窗口就是增加进程来处理需求。由于是假期,发现即使是6个窗口全开了,排队的客户还是很多,那么,这里是不是没有其它的办法了呢?效率都是逼出来的,经理发现,客户买完东西,在旁边等,当客户的汉堡(或者其它垃圾食品)准备好了,是由单独的一个人(小明)把食品递给客户,由于这个人需要把准备好的食物分别送给6个不同窗口的客户,所以效率很低。这时候经理发话了,食品准备好了,直接由窗口的售卖人员把食品给正在等在的客户,这样比较节省时间。在这里,前台售卖人员的工作就有原来的一项专门售卖商品的工作,变成了两项,就是两个进程。
进上总结,一个进程中至少有一个线程,在实际的工作中,不是所有的程序都支持多线程,也有一些程序对多进程也支持得不够好,像PHP,Node.js等都是单进程,单线程的。
下面这个python的脚本让一个进程中运行两个线程:
import time import thread def Ordering(interval): cnt = 0 while cnt<100: print '好了,你订餐成功,订餐号码是:%d号 订餐时间是:%s 请在旁边耐心等待\n\n'%(cnt, time.ctime()) time.sleep(interval) cnt+=1 thread.exit_thread() def Notice(interval): cnt = 0 while cnt<100: print '谁的号码是%d,您的餐好了,过来取一下\n'%(cnt) time.sleep(interval) cnt+=1 thread.exit_thread() def work(): #Use thread.start_new_thread() to create 2 new threads thread.start_new_thread(Ordering,(1,)) thread.start_new_thread(Notice,(5,)) if __name__=='__main__': work()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

Although distinct and distinct are related to distinction, they are used differently: distinct (adjective) describes the uniqueness of things themselves and is used to emphasize differences between things; distinct (verb) represents the distinction behavior or ability, and is used to describe the discrimination process. In programming, distinct is often used to represent the uniqueness of elements in a collection, such as deduplication operations; distinct is reflected in the design of algorithms or functions, such as distinguishing odd and even numbers. When optimizing, the distinct operation should select the appropriate algorithm and data structure, while the distinct operation should optimize the distinction between logical efficiency and pay attention to writing clear and readable code.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

C language identifiers cannot contain spaces because they can cause confusion and difficulty in maintaining. The specific rules are as follows: they must start with letters or underscores. Can contain letters, numbers, or underscores. Cannot contain illegal characters (such as special symbols).

Multithreading is an important technology in computer programming and is used to improve program execution efficiency. In the C language, there are many ways to implement multithreading, including thread libraries, POSIX threads, and Windows API.
