python - gevent and thread performance compare? -
i write test case compare thread , gevent's performance,
using multithread task like:
import time import threading def sync_task(): #do time.sleep(1) def multi_thread_run(): start = time.time() in range(10): t = threading.thread(target=sync_task) t.start() end = time.time() print("multi thread task executed in %f second"%(end-start))
print:
multi thread task executed in 0.002425 second
however, using gevent replace thread same task:
import gevent def async_task(): #do gevent.sleep(1) def async_run(): start = time.time() coroutins = [] in range(10): coroutins.append(gevent.spawn(async_task)) gevent.joinall(coroutins) end = time.time() print("async task executed in %f second"%(end-start))
print:
async task executed in 1.002012 second
in many blog post see coroutine more effective mutilthread, in case, how explain it?
that's because didn't join
thread
- means program doesn't wait thread finish executing, , instead ends immediatelly.
Comments
Post a Comment