import gevent
def task(pid):
gevent.sleep(2)
print ("task %s done"%pid)
def asynchronous():
threads = [gevent.spawn(task,i) for i in xrange(5)]
gevent.joinall(threads)
if __name__ == '__main__':
print ("asynchronous")
asynchronous()执行结果:
asynchronous
task 0 done
task 1 done
task 2 done
task 3 done
task 4 done
利用gevent实现异步执行任务
原文:http://blog.51cto.com/guoshiwei/2103510