["python","asynchronous","python-asyncio","coroutine"]
Asynchronous method call in Python?

I was wondering if there's any library for asynchronous method calls in Python. It would be great if you could do something like @async def longComputation(): <code> token = longComputation() token.registerCallback(callback_function) # alternative, polling while not token.finished(): doSomethingElse() if token.finished(): result = token.result() Or to call a non-async routine asynchronously def longComputation() <code> token = asynccall(longComputation()) It would be great to have a more refined strategy as native in the language core. Was this considered?
You can use the multiprocessing module added in Python 2.6. You can use pools of processes and then get results asynchronously with: apply_async(func[, args[, kwds[, callback]]]) E.g.: import time from multiprocessing import Pool def postprocess(result): print("finished: %s" % result) def f(x): return x*x if __name__ == '__main__': pool = Pool(processes=1) # Start a worker processes. result = pool.apply_async(f, [10], callback=postprocess) # Evaluate "f(10)" asynchronously calling callback when finished. print("waiting...") time.sleep(1) This is only one alternative. This module provides lots of facilities to achieve what you want. Also it will be really easy to make a decorator from this.