Skip to content

Python asynchronous programming demo: process/thread pool

using multiprocessing

from multiprocessing import Pool

def a(num):
    print num

if __name__ == "__main__":
    pool = Pool(3)
    for i in range(10):
        pool.apply_async(a,args=(i,))
    pool.close()
    pool.join()

using concurrent

import concurrent
from concurrent import futures
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    for item in number_list:
        executor.submit(evaluate_item,  item)

or

import concurrent
from concurrent import futures
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
    for item in number_list:
        executor.submit(evaluate_item,  item)

如同ThreadPoolExecutor一样,ProcessPoolExecutor是一个executor,使用一个线程池来并行执行任务。然而,和ThreadPoolExecutor不同的是,ProcessPoolExecutor使用了多核处理的模块,让我们可以不受GIL的限制,大大缩短执行时间。

using asyncio

import asyncio
import datetime
import time

def function_1(end_time, loop):
    print ("function_1 called")
    if (loop.time() + 1.0) < end_time:
        loop.call_later(1, function_2, end_time, loop)
    else:
        loop.stop()

def function_2(end_time, loop):
    print ("function_2 called ")
    if (loop.time() + 1.0) < end_time:
        loop.call_later(1, function_3, end_time, loop)
    else:
        loop.stop()

def function_3(end_time, loop):
    print ("function_3 called")
    if (loop.time() + 1.0) < end_time:
        loop.call_later(1, function_1, end_time, loop)
    else:
        loop.stop()

def function_4(end_time, loop):
    print ("function_4 called")
    if (loop.time() + 1.0) < end_time:
        loop.call_later(1, function_4, end_time, loop)
    else:
        loop.stop()

loop = asyncio.get_event_loop()

end_loop = loop.time() + 9.0
loop.call_soon(function_1, end_loop, loop)
# loop.call_soon(function_4, end_loop, loop)
loop.run_forever()
loop.close()

Reference