| 场景 | API | 通信机制 | |
|---|---|---|---|
| 多线程 | IO 密集型 | - joblib.threading - threading.Thread - concurrent.futures.ThreadPoolExecutor | 可以共享内存, 可以共享变量,但需要加锁 |
| 多进程 | 计算密集型 | - joblib.multiprocessing - multiprocessing.Pool - multiprocessing.apply_async - concurrent.futures.ProcessPoolExecutor | 队列,无需对queue加锁 |
GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once — Python Wiki
为何需要GIL? 因为Python解释器不是线程安全的。
# create a process
process = multiprocessing.Process(target=task)
# create a process
process = multiprocessing.Process(target=task, args=(arg1, arg2))
# run the new process
process.start()# SuperFastPython.com
# example of running a function in another process
from time import sleep
from multiprocessing import Process# a custom function that blocks for a moment
def task():# block for a momentsleep(1)# display a messageprint('This is from another process')# entry point
if __name__ == '__main__':# create a processprocess = Process(target=task)# run the processprocess.start()# wait for the process to finishprint('Waiting for the process...')process.join()
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, as_completed
if __name__ == '__main__':print('main thread is {}'.format(threading.current_thread().name))start_time = time.time()### multithread queuefrom queue import Queuequeue = Queue()tw = threading.Thread(target=write_to_queue, args=(queue,))tr = threading.Thread(target=read_from_queue, args=(queue,))tr.setDaemon(True)tw.start()tr.start()tw.join()end_time = time.time()print('total time is {}'.format(str(end_time - start_time)))
def increase(var, lock):global total_increase_timesfor i in range(1000000):if lock.acquire():var[0] += 1lock.release()total_increase_times += 1def decrease(var, lock):global total_decrease_timesfor i in range(1000000):if lock.acquire():var[0] -= 1lock.release()total_decrease_times += 1if __name__ == '__main__':print('main thread is {}'.format(threading.current_thread().name))start_time = time.time()lock = threading.Lock()var = [5]total_increase_times = 0total_decrease_times = 0t1 = threading.Thread(target=increase, args=(var, lock))t2 = threading.Thread(target=decrease, args=(var, lock))t1.start()t2.start()t1.join()t2.join()print(var)print('total increase times: {}'.format(str(total_increase_times)))print('total decrease times: {}'.format(str(total_decrease_times)))end_time = time.time()print('total time is {}'.format(str(end_time - start_time)))
上一篇:阿隆索命硬!2-4落后连扳3球逆转+读秒绝杀,赛季37场不败冲3冠王 阿隆索命硬!2-4落后连扳3球逆转 读秒绝杀,赛季37场不败冲3冠王
下一篇:欧联神仙局!让二追三+补时2球绝杀 勒沃库森37场不败晋级八强 欧联神仙局!让二追三 补时2球绝杀 勒沃库森37场不败晋级八强