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):
group.spawn(writer, options.duration, bucket,
name=x, name=x,
queue=q, queue=q,
file_size=options.file_size, file_size=options.file_size,
file_stddev=options.stddev, file_stddev=options.stddev,
quantity=options.quantity, quantity=options.quantity,
file_name_seed=r, file_name_seed=r,
) for x in xrange(options.num_writers)] )
greenlets += [gevent.spawn(reader, options.duration, bucket, for x in xrange(options.num_readers):
group.spawn(reader, options.duration, bucket,
name=x, name=x,
queue=q, queue=q,
) for x in xrange(options.num_readers)] )
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: