Skip to content

Instantly share code, notes, and snippets.

@sublee
Created December 12, 2012 05:04
Show Gist options
  • Save sublee/4265015 to your computer and use it in GitHub Desktop.
Save sublee/4265015 to your computer and use it in GitHub Desktop.
"""
spawn...
3.56747082911
builtin_function_or_method 100727
18.2844139451
dict 200500
20.1863144234
dict 300500
22.0894001079
dict 400500
23.9884560911
dict 500500
done spawning
all jobs completed
25.6268852187
function 1483
25.6268852187
function 1483
25.6268852187
function 1483
"""
import os
import gevent
import objgraph
import psutil
ps = psutil.Process(os.getpid())
def f(x):
gevent.sleep(1)
def main():
print 'spawn...'
jobs = []
for y in xrange(5):
for x in xrange(100000):
jobs.append(gevent.spawn(f, x))
print ' ', ps.get_memory_percent()
print ' ', ' '.join(map(str, objgraph.most_common_types(1)[0]))
gevent.sleep(2)
print 'done spawning'
while True:
if jobs is not None and all([job.ready() for job in jobs]):
print 'all jobs completed'
jobs = None
print ' ', ps.get_memory_percent()
print ' ', ' '.join(map(str, objgraph.most_common_types(1)[0]))
gevent.sleep(2)
gevent.joinall([gevent.spawn(main)])
import os
import gevent
def f(x):
gevent.sleep(1)
def main():
print 'pid =', os.getpid()
print 'spawn...'
jobs = []
for y in xrange(5):
for x in xrange(100000):
jobs.append(gevent.spawn(f, x))
gevent.sleep(2)
print 'done spawning'
# loop infinitely
while True:
if jobs is not None and all([job.ready() for job in jobs]):
print 'all jobs completed'
jobs = None
gevent.sleep(2)
gevent.joinall([gevent.spawn(main)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment