What's the correct way to wait "forever"

3,776 views
Skip to first unread message

vitaly

unread,
May 20, 2013, 8:01:28 PM5/20/13
to gev...@googlegroups.com
A gevent Greenlet in my app starts several objects, then needs to wait "forever" (until the main greenlet kills it), then stops those objects in the finally block. Is there a built-in gevent function that blocks forever, or should I create a gevent.event.Event instance (that will never get set) and wait on it instead?

Thank you,
Vitaly

AM

unread,
May 20, 2013, 8:09:47 PM5/20/13
to gev...@googlegroups.com, vitaly
> --
> 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+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
greenlet = gevent.spawn(...) # or anything else to spawn it
greenlet.join()

will wait for the greenlet to exit.

HTH
AM

Matt Billenstein

unread,
May 20, 2013, 8:11:18 PM5/20/13
to gev...@googlegroups.com
Take a look at serve_forever() in baseserver.py

m

On Mon, May 20, 2013 at 05:01:28PM -0700, vitaly wrote:
> A gevent Greenlet in my app starts several objects, then needs to wait
> "forever" (until the main greenlet kills it), then stops those objects in
> the finally block. Is there a built-in gevent function that blocks
> forever, or should I create a gevent.event.Event instance (that will never
> get set) and wait on it instead?
> Thank you,
> Vitaly
>
> --
> 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+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
Matt Billenstein
ma...@vazor.com
http://www.vazor.com/

vitaly

unread,
May 21, 2013, 1:16:40 AM5/21/13
to gev...@googlegroups.com, Matt Billenstein
Thanks everyone for the initial attempts, but those don't work in my scenario because my app does not have access to the underlying greenlets. Let me provide an example to clarify:

def mainGreenthread(self):
  try:
    self.objX.start()
    self.objY.start()
    
    # NEEDED SOMETHING THAT ACCOMPLISHES gevent-friendly "wait forever":
    gevent_wait_forever()
  finally:
    self.objY.stop()
    self.objX.stop()

def start(self):
  self.mainGreenlet = gevent.spawn(self.mainGreenthread)

def stop(self):
  self.mainGreenlet.kill()



So, I was trying to find out whether there is a single function in gevent to accomplish "gevent_wait_forever". I know I can do this via gevent.event.Event().wait(), but it doesn't look terribly elegant, and I was hoping there might be some more elegant alternative.

Liam Slusser

unread,
May 21, 2013, 2:36:47 AM5/21/13
to gev...@googlegroups.com
You could always do something like...

while True:
    gevent.sleep(60)

liam


On Mon, May 20, 2013 at 5:15 PM, Harry Waye <hw...@arachnys.com> wrote:
It seems by definition you're not blocking forever, instead you're waiting on the main thread sending a kill "event" so yes, an event would be applicable.  Then you'll want to join() that thread to give it time to "stop" its objects.

Matthias Urlichs

unread,
May 21, 2013, 2:38:40 AM5/21/13
to gev...@googlegroups.com, Matt Billenstein
Hi,

vitaly:
> # NEEDED SOMETHING THAT ACCOMPLISHES gevent-friendly "wait forever":

Create a queue and get() from it

> def stop(self):
> self.mainGreenlet.kill()

that_queue.put(None)

--
-- Matthias Urlichs

vitaly

unread,
May 21, 2013, 9:35:19 AM5/21/13
to gev...@googlegroups.com, Matt Billenstein
Thank you

Denis Bilenko

unread,
May 21, 2013, 9:36:42 AM5/21/13
to gev...@googlegroups.com
There's gevent.wait(): it puts main greenlet to sleep while there's
something to do (started servers, open connections, spawned
greenlets).

vitaly

unread,
May 21, 2013, 10:54:28 AM5/21/13
to gev...@googlegroups.com
On Tuesday, May 21, 2013 6:36:42 AM UTC-7, Denis Bilenko wrote:
There's gevent.wait(): it puts main greenlet to sleep while there's
something to do (started servers, open connections, spawned
greenlets).

gevent.wait() looks like exactly what I needed. I'm still on 1.0b2, so I will be looking forward to gevent.wait() after upgrading.

Thank you,
Vtaly

Ian Epperson

unread,
May 21, 2013, 2:03:02 PM5/21/13
to gev...@googlegroups.com, Matt Billenstein
gevent.wait() is in every version of gevent - you just need to tell it how long to wait - Liam suggested a while True: gevent.wait(60) which would work for you.  However, it's not really as clean (in my opinion) as just using an event for shutdown.  For instance:

from gevent.event import Event

self.shutdown = Event()

def mainGreenthread(self):
  try:
    self.objX.start()
    self.objY.start()
    
    # NEEDED SOMETHING THAT ACCOMPLISHES gevent-friendly "wait forever":
    self.shutdown.wait()
  finally:
    self.objY.stop()
    self.objX.stop()

def start(self):
  self.mainGreenlet = gevent.spawn(self.mainGreenthread)

def stop(self):
  self.shutdown.set()

Ian.


--
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+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
This email is intended for the use of the individual addressee(s) named above and may contain information that is confidential, privileged or unsuitable for overly sensitive persons with low self-esteem, no sense of humor or irrational religious beliefs. If you are not the intended recipient, any dissemination, distribution or copying of this email is not authorized (either explicitly or implicitly) and constitutes an irritating social faux pas. Unless the word absquatulation has been used in its correct context somewhere other than in this warning, it does not have any legal or grammatical use and may be ignored. No animals were harmed in the transmission of this email, although the yorkshire terrier next door is living on borrowed time, let me tell you. Those of you with an overwhelming fear of the unknown will be gratified to learn that there is no hidden message revealed by reading this warning backwards, so just ignore that Alert Notice from Microsoft: However, by pouring a complete circle of salt around yourself and your computer you can ensure that no harm befalls you and your pets. If you have received this email in error, please add some nutmeg and egg whites and place it in a warm oven for 40 minutes. Whisk briefly and let it stand for 2 hours before icing.

Lars Hansson

unread,
Sep 5, 2013, 5:49:59 AM9/5/13
to gev...@googlegroups.com
This works pretty well for me:

def signal_shutdown():
    raise KeyboardInterrupt

def main():
    <do stuff>
    gevent.signal(signal.SIGTERM, signal_shutdown)
    forever = gevent.event.Event()
    try:
        forever.wait()
    except KeyboardInterrupt:
        <do stuff>

---
Lars
Reply all
Reply to author
Forward
0 new messages