python - Generator as Callback -
i looking way stuff callback generator,
or generator inherit callback.
when callback called, generator should run, yield
ing data.
def my_callback(some_data): my_generator.execute(some_data) # do? def my_generator(): while true: yield some_data # my_callback # main program x in my_generator: print(x) # pseudo python code.
while use queue , thread run , callback, in case running inside python extension of cli calls my_callback(data)
.
result, can't use threading, python execute callback. afterwards c part of cli stuff again, , python not executed.
edit: register 'poll' function, called periodically. putting wait in there give @ least thread-queue construction time execute. feels dirty actual code.
the callback can send data coroutine can send along again if needed. not sure if looking question made me think of coroutines.
def my_callback(some_data): cr.send(some_data) # do? def my_coroutine(target): n = 0 while true: data = (yield) # my_callback print 'my_callback sent: {}'.format(data) target.send(data) # my_printer def my_printer(prefix): while true: s = (yield) # my_coroutine print prefix + s mp = my_printer('\t\t\t') mp.next() # advance first yield cr = my_coroutine(mp) cr.next() # advance first yield my_callback('foo') n in xrange(3): my_callback('foo' + str(n)) cr.close() mp.close() >>> my_callback sent: foo foo my_callback sent: foo0 foo0 my_callback sent: foo1 foo1 my_callback sent: foo2 foo2 >>>
if callback execution asynchronous, i'm not sure happen if coroutine wasn't back to first yield
, callback tries send something, might throw valueerror: generator executing
exception.
Comments
Post a Comment