我正在尝试为一个项目提供小型Web服务,通过该服务我必须使用python库,通过该库我们必须执行CPU密集型任务。
我计划使用Tornado concurrent.futures.ProcessPoolExecutor
将任务分流到可以解决问题的单独流程中
Web服务还可能在处理正在进行时接收请求,以获取处理状态,因此,在所述子流程中,我计划生成两个线程,一个线程负责执行,而另一个线程可以返回需要时执行状态。
我计划使用concurrent.futures.ThreadPoolExecutor
,并计划将未来包装在asyncio.Future
中以返回结果。以下几行:
import math
import concurrent.futures
import asyncio
from random import randint
from time import sleep
def is_prime(n):
x = randint(0, 5)
print("Random Sleep {} for {}".format(x, n))
sleep(x)
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True
'''Wrapping corouting which waits for return from process pool.'''
async def get_result(executor, n):
loop = asyncio.get_event_loop()
prime = await loop.run_in_executor(executor, is_prime, n)
return n, prime
'''Scheduling the run in the asyncio event loop'''
async def main():
prime_candidates = [
112272535095293,
112582705942171,
112272535095293,
115280095190773,
115797848077099,
1099726899285419,
17,
4]
# create the process pool
with concurrent.futures.ThreadPoolExecutor(4) as executor:
# Calling the asyncio coroutines returns futures.
futures = [get_result(executor, n) for n in prime_candidates]
# As futures are completed they are returned and the result can be obtained
for i, future in enumerate(asyncio.as_completed(futures)):
n, prime = await future
if prime:
print("{}: {} is prime".format(i,n))
else:
print("{}: {} is not prime".format(i,n))
if __name__=='__main__':
# This creates the event loop and runs main in the loop until main returns.
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
但是,我不知道如何将事件循环与从concurrent.futures.ProcessPoolExecutor
接收任务的类相关联。我没有可以等待完成的功能。
我是Python的新手,这超出了我的深度