Asynchronous method call in Python?


Something like:

import threading

thr = threading.Thread(target=foo, args=(), kwargs={})
thr.start() # Will run "foo"
thr.is_alive() # Will return whether foo is running currently
thr.join() # Will wait till "foo" is done

See the documentation at for more details; this code should work for Python 3 as well.


I was wondering if there's any library for asynchronous method calls in Python. It would be great if you could do something like

def longComputation():

token = longComputation()
# alternative, polling
while not token.finished():
    if token.finished():
        result = token.result()

Or to call a non-async routine asynchronously

def longComputation()

token = asynccall(longComputation())

It would be great to have a more refined strategy as native in the language core. Was this considered?

You could use eventlet. It lets you write what appears to be synchronous code, but have it operate asynchronously over the network.

Here's an example of a super minimal crawler:

urls = ["",

import eventlet
from import urllib2

def fetch(url):

  return urllib2.urlopen(url).read()

pool = eventlet.GreenPool()

for body in pool.imap(fetch, urls):
  print "got body", len(body)

It's not in the language core, but a very mature library that does what you want is Twisted. It introduces the Deferred object, which you can attach callbacks or error handlers ("errbacks") to. A Deferred is basically a "promise" that a function will have a result eventually.

You can use process. If you want to run it forever use while (like networking) in you function:

from multiprocessing import Process
def foo():
    while 1:
        # Do something

p = Process(target = foo)

if you just want to run it one time, do like that:

from multiprocessing import Process
def foo():
    # Do something

p = Process(target = foo)


import threading, time

def f():
    print "f started"
    print "f finished"


Is there any reason not to use threads? You can use the threading class. Instead of finished() function use the isAlive(). The result() function could join() the thread and retrieve the result. And, if you can, override the run() and __init__ functions to call the function specified in the constructor and save the value somewhere to the instance of the class.