You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is the private eliot_friendly_generator_function, which is almost what I need, but it initializes the context on the first call of next/send, rather than on creation.
My use cases is wrapping a generator provided to twisted's Cooperator.
The text was updated successfully, but these errors were encountered:
Just for context, that code was originally written by @exarkun and I don't actually quite understand it, though I'm sure I could with some effort.
Some options:
Make that API public and have the functionality you want.
Make a variant of Cooperator that has a context-per-iterator using contextvars (https://docs.python.org/3/library/contextvars.html). Eliot automatically derives its own context from that, e.g. for asyncio, so it should Just Work.
No doubt there are other options.
My preference is item 2, since making generic context system is what contextvars is for, and I imagine it might be more broadly useful as a feature. And it's probably only a few lines of code:
Create a Context per iterator.
When __next__ is called on iterator, wrap it with context.run().
Something like:
fromcontextvarsimportContextfromtwisted.internet.taskimportCooperatorclassIteratorWrapper:
def__init__(self, it):
self.it=itself.context=Context()
def__next__(self):
returnself.context.run(self.it.__next__)
classCooperatorWithContext:
def__init__(self):
self._coop=Cooperator()
defcoiterate(self, iterator):
returnself._coop.coiterate(IteratorWrapper(iterator))
# ... wrap other methods too ...
There is the private
eliot_friendly_generator_function
, which is almost what I need, but it initializes the context on the first call ofnext
/send
, rather than on creation.My use cases is wrapping a generator provided to twisted's
Cooperator
.The text was updated successfully, but these errors were encountered: