Python异步调用函数()?

2024-09-26 21:43:59 发布

您现在位置:Python中文网/ 问答频道 /正文

异步调用函数的最短方法是什么

用户应始终能够输入新值; 但是每个action()必须排队

def action(i):
   #takes a long time to be achieve

while True:
    i = raw_input("Input your value: ")
    action(i)

Tags: to方法用户truetimedefactionbe
1条回答
网友
1楼 · 发布于 2024-09-26 21:43:59

使用多处理模块:

from multiprocessing import Pool

def action(i):
   #takes a long time to be achieve

worker_pool = Pool(processes=1)
while True:
    i = raw_input("Input your value: ")
    result = worker_pool.apply_async(action, [i], callback)

你也可以用芹菜做背景任务:

@celery_app.task(bind=True,max_retries=None)
def action(i):
   #takes a long time to be achieve

while True:
    i = raw_input("Input your value: ")
    action.apply_async(args=[i])

相关问题 更多 >

    热门问题