Tristan
Tristan

Reputation: 6906

Turning a generator into a callback with an eventloop in Python

Is there a simple way in python to turn a generator into a callback from an event loop, without using threads?

I'd like to change the code

for x in generator:
    callback(x)

to something that can be used with Tornado's event loop. The problem with the above code is that it blocks the current thread if the generator blocks.

Upvotes: 2

Views: 321

Answers (1)

theheadofabroom
theheadofabroom

Reputation: 22089

You could try multiprocessing, but that is one huge sledgehammer for that tiny little nut - you're better looking for another foodsource, i.e. make sure your callback is non-blocking, because even using threads you're going to end up fighting the GIL.

Upvotes: 1

Related Questions