Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, generators are probably a red herring; don't worry about them.</p> <p>The canonical way to solve this kind producer-consumer problem in Python is using the built-in <a href="http://docs.python.org/library/queue.html" rel="nofollow"><code>queue</code></a> module. It acts as an intermediary, allowing your producer thread to keep grabbing/processing data from the kernel into the queue, and your consumer thread to send queue data to the server, without their respective blocking I/O calls interfering with one another.</p> <p>Here's a sketch of the basic idea, without the details filled in:</p> <pre><code>from queue import Queue class Application(object): def __init__(self): self.q = Queue() self.running = False # From kernel to queue def produce(self): while self.running: data = read_from_kernel() self.q.put(data) # From queue to server def consume(self): while self.running: data = self.q.get() send_to_server(data) # Start producer thread, then consume def run(): try: self.running = True producer = Thread(target=self.produce) producer.start() self.consume() finally: self.running = False </code></pre> <p>If <code>self.running</code> is set to False, the above code's <code>produce</code> method will still block inside the <code>read_from_kernel</code> until its next return before exiting itself, but there's little Python can do about that. Whatever system call you use must support this somehow: if it's an actual <code>read</code>, for example, your options would include:</p> <ul> <li>A short timeout, plus retry handling</li> <li>Non-blocking I/O (but in this case you might want to investigate a framework based around this, like <a href="http://twistedmatrix.com/" rel="nofollow">Twisted Python</a>)</li> </ul>
    singulars
    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.
    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