Process
涉及模块:multiprocessing
Process
p = Process()
p.start()
p.join()
from multiprocessing import Processimport os# 子进程要执行的代码def run_proc(name): print('Run child process %s (%s)...' % (name, os.getpid()))if __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Process(target=run_proc, args=('test',)) print('Child process will start.') p.start() p.join() print('Child process end.')
Pool
p=Pool(5)
p.apply_async(func,**args)
p.close()
p.join()
from multiprocessing import Poolimport os, time, randomdef long_time_task(name): print('Run task %s (%s)...' % (name, os.getpid())) start = time.time() time.sleep(random.random() * 3) end = time.time() print('Task %s runs %0.2f seconds.' % (name, (end - start)))if __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses done.')
Thread
涉及模块:threading
- Thread
- t = threading.Thread(target=xxx,name=xxx,args=(xxx,xxx))
- t.start()
- t.join()
- threading.curent_thread().name
- l = threading.Lock()
- l.acquire()
- l.release()
import time, threading# 新线程执行的代码:def loop(s): print(s) print('thread %s is running...' % threading.current_thread().name) n = 0 while n < 5: n = n + 1 print('thread %s >>> %s' % (threading.current_thread().name, n)) time.sleep(1) print('thread %s ended.' % threading.current_thread().name)print('thread %s is running...' % threading.current_thread().name)t = threading.Thread(target=loop, name='LoopThread', args=('a',))t.start()t.join()print('thread %s ended.' % threading.current_thread().name)
- ThreadLocal
- 线程中共享所有的全局变量,需注意是否会死锁
- 局部只有当前线程可以进行操作,但调用会很麻烦
- local = threading.local()
import threading# 创建全局ThreadLocal对象:local_school = threading.local()def process_student(): # 获取当前线程关联的student: std = local_school.student print('Hello, %s (in %s)' % (std, threading.current_thread().name))def process_thread(name): # 绑定ThreadLocal的student: local_school.student = name process_student()t1 = threading.Thread(target= process_thread, args=('Alice',), name='Thread-A')t2 = threading.Thread(target= process_thread, args=('Bob',), name='Thread-B')t1.start()t2.start()t1.join()t2.join()
第三方进程与线程模块
涉及模块:concurrent.futures
- ThreadPoolExecutor、ProcessPoolExecutor
- submit
- map
- concurrent.futures.as_completed
- zip
- 拼接
- zip((1,2),(10,20)) ->((1,10),(2,20))
def sim_add(a): return a[0]+a[1] from concurrent.futures import ThreadPoolExecutorimport concurrent.futureswith ThreadPoolExecutor(max_workers=1) as t1: future1 = t1.submit(sim_add,(1,2)) print(future1.result())print()var = [(1,2),(10,20),(100,200)]with ThreadPoolExecutor(max_workers=2) as t: future = {t.submit(sim_add,tem): tem for tem in var} for parameter,res in zip(var,concurrent.futures.as_completed(future)): print("sim_add %s,return %s" % (parameter,res)) # 多次submit,不接收结果但是又想全部执行完 # concurrent.futures.wait()print()with ThreadPoolExecutor(max_workers=2) as t: for f in t.map(sim_add,[(1,2),(2,3)]): print(f)