Use gevent Groups to manage groups of workers.

This mostly adds convenience functions, but it's nice to have.
This commit is contained in:
Tommi Virtanen 2011-07-20 09:42:39 -07:00
parent 510b6e91d4
commit dff6b568e4

View file

@ -1,6 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
import gevent import gevent
import gevent.pool
import gevent.queue import gevent.queue
import gevent.monkey; gevent.monkey.patch_all() import gevent.monkey; gevent.monkey.patch_all()
import optparse import optparse
@ -137,19 +138,21 @@ def main():
# main work # main work
print "Using file size: {size} +- {stddev}".format(size=options.file_size, stddev=options.stddev) print "Using file size: {size} +- {stddev}".format(size=options.file_size, stddev=options.stddev)
print "Spawning {r} readers and {w} writers...".format(r=options.num_readers, w=options.num_writers) print "Spawning {r} readers and {w} writers...".format(r=options.num_readers, w=options.num_writers)
greenlets = [] group = gevent.pool.Group()
greenlets += [gevent.spawn(writer, options.duration, bucket, for x in xrange(options.num_writers):
name=x, group.spawn(writer, options.duration, bucket,
queue=q, name=x,
file_size=options.file_size, queue=q,
file_stddev=options.stddev, file_size=options.file_size,
quantity=options.quantity, file_stddev=options.stddev,
file_name_seed=r, quantity=options.quantity,
) for x in xrange(options.num_writers)] file_name_seed=r,
greenlets += [gevent.spawn(reader, options.duration, bucket, )
name=x, for x in xrange(options.num_readers):
queue=q, group.spawn(reader, options.duration, bucket,
) for x in xrange(options.num_readers)] name=x,
queue=q,
)
gevent.spawn_later(options.duration, lambda: q.put(StopIteration)) gevent.spawn_later(options.duration, lambda: q.put(StopIteration))
total_read = 0 total_read = 0
@ -192,7 +195,7 @@ def main():
percent=(100.0*write_failure/max(write_failure+write_success, 1)), percent=(100.0*write_failure/max(write_failure+write_success, 1)),
) )
gevent.joinall(greenlets, timeout=1) group.join(timeout=1)
except Exception as e: except Exception as e:
print e print e
finally: finally: