Python之线程、进程和协程

一、线程概念:

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

二、python中线程的使用:

#!/usr/bin/env python
# -*- coding=utf-8 -*-

import threading
import time

def show(arg):
    time.sleep(1)
    print("thread"+str(arg))

for i in range(10):
    t = threading.Thread(target=show,args=(i,))
    t.start()

print("main thread stop")

输出结果如下:

[root@hadoop01 Blog]# python thread_pool.py 
main thread stop
thread0
thread1
thread2
thread3
thread4
thread5
thread6
thread7
thread8
thread9

未完待续。。。

原文地址:https://www.cnblogs.com/Richardzhu/p/5310672.html