簡體   English   中英

Python-異步調用function()嗎?

[英]Python - Asynchronously call of a function()?

異步調用函數的最短方法是什么?

用戶應始終能夠輸入新值; 但是每個action()必須排隊

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

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

使用多處理模塊:

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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM