Created
June 7, 2013 08:24
-
-
Save fspot/5727831 to your computer and use it in GitHub Desktop.
Example script using several process in order to consume messages put in a message queue.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
""" | |
Example script using several process in order to consume | |
messages put in a message queue. | |
""" | |
from multiprocessing import Process, Queue, cpu_count | |
def push_msgs(queue, nb_msgs, nb_pullers): | |
print "<pusher process starts>" | |
for i in xrange(nb_msgs): | |
queue.put([i, 42, 'hello']) | |
for i in xrange(nb_pullers): | |
queue.put("END") | |
print "<pusher process exits>" | |
def pull_msgs(queue): | |
print "<puller process starts>" | |
treated_msgs = 0 | |
while True: | |
msg = queue.get() | |
if msg == "END": | |
break | |
treated_msgs += 1 | |
print "<puller process exit after %d msgs>" % treated_msgs | |
def main(): | |
print "<main process starts>" | |
q = Queue() | |
nb_messages = 10000 | |
nb_pullers = cpu_count() - 1 | |
msg_pusher_process = Process(target=push_msgs, args=(q, nb_messages, nb_pullers)) | |
msg_pusher_process.start() | |
for i in xrange(nb_pullers): | |
msg_puller_process = Process(target=pull_msgs, args=(q,)) | |
msg_puller_process.start() | |
print "<main process exits>" | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment