[Chicago] gevent question

Osman Siddique osiddique at gmail.com
Tue Jul 28 07:14:32 CEST 2015


hi best users group ever,

any help would be greatly appreciated. i'm trying to write something that
sends out a bunch of messages at once. i have one solution that works, but
i feel like it's slow b/c it takes ~4 seconds to send 3000 http post
requests on my 2012 my macbook air. admittedly, i'm new to the exciting
world of http so maybe this is normal.

here's the solution that doesn't work.

import requests, json, multiprocessing, gevent
from gevent import monkey

monkey.patch_socket()

def send(s, data):
    s.post("http://127.0.0.1:8080", data)
    print data

def worker(id):
    s = requests.session()
    for i in range(1, 1000):
        data = json.dumps({'a':True,'b':123.0,'c':i,'d':id})
        gevent.spawn(send, s, data)
    data = json.dumps({'a':True,'b':123.0,'d':id})
    send(s, data)

if __name__ == '__main__':
    p = multiprocessing.Pool(5)
    p.map(worker, [1, 2, 3])

my server running on the other side only seems to see a few of the first
requests from the for loop. after that, the next thing it sees are the
requests outside of the loop. basically wondering why i don't see that
'print data' command in the send func being executed.

thanks,
osman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150728/b5778787/attachment.html>


More information about the Chicago mailing list