Discussion:
[gevent] want to pass an argument to greenlets spawned by streamserver
Jerry S
2018-04-08 20:07:39 UTC
Permalink
Hi,

I want to be able to pass a dictionary into each greenlet that is spawned
by a new tcp connection. I was thinking of subclassing gevent.StreamServer
and replacing the init_socket routine. Is that the best way to do this?

thanks in advance,
jerry
--
You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gevent+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason Madden
2018-04-09 13:24:52 UTC
Permalink
Hi,
I want to be able to pass a dictionary into each greenlet that is spawned by a new tcp connection. I was thinking of subclassing gevent.StreamServer and replacing the init_socket routine. Is that the best way to do this?
I doubt it. That's only called once when the server starts, not for every greenlet created to handle each connection. If you want something called for every greenlet, pass in the `spawn` argument, e.g.,

def my_spawn(*args):
return Greenlet.spawn(*args, my_dict=get_the_dict())

StreamServer(("localhost", 8080), handle=my_handler, spawn=my_spawn)

Alternately (and probably preferably), you may be able to do what you want in your handler function, where gevent.getcurrent() is the executing greenlet.
--
You received this message because you are subscribed to the Google Groups "gevent: coroutine-based Python network library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gevent+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...