Python之线程与进程相关介绍
Python 线程与进程
线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
使用 threading 模块
方法一:
import threading import time def foo(n): print('foo %s'%n) time.sleep(1) print('end foo') def bar(n): print('bar %s'%n) time.sleep(2) print('end bar') t1 = threading.Thread(target=foo, args=(1,)) t2 = threading.Thread(target=bar, args=(2,)) t1.start() t2.start() print('........in the main..........')
运行结果:
foo 1 bar 2 ........in the main.......... end foo end bar
方法二:
import time, threading class MyThread(threading.Thread): def __init__(self, num): threading.Thread.__init__(self) self.num = num def run(self): #定义线程要运行的函数 print("running on number:%s" % self.num) time.sleep(3) if __name__ == '__main__': t1 = MyThread(1) t2 = MyThread(2) t1.start() t2.start()
运行结果:
running on number:1 running on number:2
join 方法使得主线程等待子线程完成才继续
import threading import time begin = time.time() def foo(n): print('foo %s'%n) time.sleep(1) print('end foo') def bar(n): print('bar %s'%n) time.sleep(2) print('end bar') t1 = threading.Thread(target=foo, args=(1,)) t2 = threading.Thread(target=bar, args=(2,)) t1.start() t2.start() t1.join() t2.join() print('........in the main..........')
运行结果:
foo 1 bar 2 end foo end bar ........in the main..........
相关推荐:《Python相关教程》
在计算密集型任务中串行与多线程进行对比
import threading, time begin = time.time() def add(n): sum = 0 for i in range(n): sum += i print(sum) add(100000000) add(200000000) end = time.time() print(end-begin)
运行结果:
4999999950000000 19999999900000000 17.66856598854065
import threading, time begin = time.time() def add(n): sum = 0 for i in range(n): sum += i print(sum) t1 = threading.Thread(target=add, args=(100000000,)) t1.start() t2 = threading.Thread(target=add, args=(200000000,)) t2.start() t1.join() t2.join() end = time.time() print(end-begin)
运行结果:
4999999950000000 19999999900000000 21.088160276412964 # 结果为串行运行比多线程运行更快
Cpython 中有 GIL (Global Interpreter Lock,全局解释器锁),所以在同一时刻,只能有一个线程进入调度。如果任务是IO密集型的,可以使用多线程;如果任务是计算密集型的,最优方法是改成 C。
setDaemon()
调用该方法只要是主线程完成,不管子线程是否完成都要和主线程一起退出。
threading.currentThread()
返回当前的线程变量。
threading.active_count()
返回正在运行的线程数量。
import threading, time from time import ctime,sleep def music(func): print(threading.current_thread()) for i in range(2): print("Begin listening to %s. %s" %(func, ctime())) sleep(2) print("end listening %s" %ctime()) def movie(func): print(threading.current_thread()) for i in range(2): print("Begin watching at the %s %s" %{func, ctime()}) sleep(4) print("end watching %s" %ctime()) threads = [] t1 = threading.Thread(target=music, args=('klvchen',)) threads.append(t1) t2 = threading.Thread(target=movie, args=('lili',)) threads.append(t2) if __name__ == '__main__': for t in threads: t.setDaemon(True) t.start() print(threading.current_thread()) print(threading.active_count()) print("all over %s" %ctime())
运行结果:
<Thread(Thread-1, started daemon 5856)> Begin listening to klvchen. Wed Jul 11 23:43:51 2018 <Thread(Thread-2, started daemon 9124)> <_MainThread(MainThread, started 9444)> 3 all over Wed Jul 11 23:43:51 2018
来源:PY学习网:原文地址:https://www.py.cn/article.html