Note that there are some explanatory texts on larger screens.

plurals
  1. POObtaining data from blocking source in Gevent loop callback
    primarykey
    data
    text
    <p>I'm trying to use Pycurl with Gevent in order to perform HTTP uploads. For this I'm relying on the <a href="https://bitbucket.org/xitrium/gevent-curl/src/82eb2d360998/geventcurl.py" rel="nofollow">geventcurl.py</a> module, which alters libcurl's <em>Multi</em> API to use Gevent's event loop.</p> <p>The problem resides in the <em>READFUNCTION</em> callback. This callback is executed in the <em>HUB</em> context, and so we cannot <em>wait()</em>, but this callback must return the data to upload and in my case this data comes from a blocking source.</p> <p>Here is a snippet of code that demonstrates the problem:</p> <pre><code>#!/usr/bin/env python from gevent import monkey; monkey.patch_all() import gevent from gevent.queue import Queue import pycurl from geventcurl import Curl URL = 'http://localhost:8000/' class QueueReader: def __init__(self, q): self.q = q def read_callback(self, size): return self.q.get(timeout=10) dataq = Queue(10) c = Curl() c.setopt(pycurl.URL, URL) c.setopt(pycurl.UPLOAD, 1) c.setopt(pycurl.READFUNCTION, QueueReader(dataq).read_callback) # Start transfer g = gevent.spawn(c.perform) for i in xrange(10): dataq.put(str(i)) gevent.sleep(1) g.join() c.close() </code></pre> <p>To run the snippet you just need to have something listening on localhost:8000, <code>nc -l 8000</code> will do. What happens is that, since <code>read_callback()</code> is executed in HUB context, it will not wait, if the queue is empty it will immediately raise an Empy exception. Using an AsyncResult also does not help since we have to wait() for the result.</p> <p>Is there any way of fetching data from a possibly blocking source in the event loop callback?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload