> On Aug 30, 2016, at 21:41,
kst...@lyft.com wrote:
>
> It seems like the best solution here is to override/decorate the run method:
>
> class FlaskContextPropagatingGreenlet(Greenlet):
> def __init__(self, *args, **kwargs):
> if flask.request:
> self.run = flask.copy_current_request_context(self.run)
> super(FlaskContextPropagatingGreenlet, self).__init__(*args, **kwargs)
>
> Is there anything wrong with this approach?
Other than being forbidden by the documentation, you mean? :)
gevent.Greenlet.run is tightly coupled to the lifecycle implementation of the greenlet. Certain APIs will not work before it is executed, and certain APIs will not work after it is executed. Which ones and the nature of the coupling is subject to change at any time. What you propose above will *probably* work, depending on the details, but it may break at any time.
Given that `_run` is the documented attribute in which to execute user code, it seems like something like this might handle both cases:
class AGreenlet(Greenlet):
def __init__(self, *args, **kwargs):
# Let it set up _run based on arguments
Greenlet.__init__(self, *args, **kwargs)
# Now we decorate it
self._run = decorate_run(self._run)