Home Backend Development Python Tutorial 线程和进程的区别及Python代码实例

线程和进程的区别及Python代码实例

Jun 10, 2016 pm 03:18 PM
python the difference thread process

在程序猿的世界中,线程和进程是一个很重要的概念,很多人经常弄不清线程和进程到底是什么,有什么区别,本文试图来解释一下线程和进程。首先来看一下概念:

进程(英语:process),是计算机中已运行程序的实体。进程为曾经是分时系统的基本运作单位。在面向进程设计的系统(如早期的UNIX,Linux 2.4及更早的版本)中,进程是程序的基本执行实体;在面向线程设计的系统(如当代多数操作系统、Linux 2.6及更新的版本)中,进程本身不是基本运行单位,而是线程的容器。程序本身只是指令、数据及其组织形式的描述,进程才是程序(那些指令和数据)的真正运行实例。–维基百科

线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。–维基百科

概念太吓人了,先来看一下进程,这个相对于线程来说还是稍微好理解一点的。进程,是程序运行的实体,这句话的意思是,程序是存放在硬盘中的,当这个程序运行时,就会产生若干个进程,并且这个进程是可见的,在windows8的任务管理器中,我们看到有下列进程:

那么什么是线程呢?如果你编写过程序,特别是类似windows的批处理这样的逻辑控制语句少的语言,就能明显感觉到,当运行一个程序是,实际上整个运行的过程是从头部一直运行到尾部。例如有这样一段批处理的代码,提示用户输入姓名,然后根据姓名输出问候:

@echo off
set /p name=请输入您的姓名: 
cls
echo 你好,%name
pause
Copy after login

这段小程序就是自上而下执行,执行完毕则退出。不管是批处理,像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()
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

See all articles