What's the shortest way to call a function asyncronously?
The user should always be able to input a new value; But each action() must be queued
def action(i):
#takes a long time to be achieve
while True:
i = raw_input("Input your value: ")
action(i)
Use Multiprocessing module:
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)
You can also use celery for background tasks:
@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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.