Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:14:55+00:00 2026-06-13T10:14:55+00:00

While double checking that threading.Condition is correctly monkey patched, I noticed that a monkeypatched

  • 0

While double checking that threading.Condition is correctly monkey patched, I noticed that a monkeypatched threading.Thread(…).start() behaves differently from gevent.spawn(…).

Consider:

from gevent import monkey; monkey.patch_all()
from threading import Thread, Condition
import gevent

cv = Condition()

def wait_on_cv(x):
    cv.acquire()
    cv.wait()
    print "Here:", x
    cv.release()

# XXX: This code yields "This operation would block forever" when joining the first thread
threads = [ gevent.spawn(wait_on_cv, x) for x in range(10) ]

"""
# XXX: This code, which seems semantically similar, works correctly
threads = [ Thread(target=wait_on_cv, args=(x, )) for x in range(10) ]
for t in threads:
    t.start()
"""

cv.acquire()
cv.notify_all()
print "Notified!"
cv.release()

for x, thread in enumerate(threads):
    print "Joining", x
    thread.join()

Note, specifically, the two comments starting with XXX.

When using the first line (with gevent.spawn), the first thread.join() raises an exception:

Notified!
Joining 0
Traceback (most recent call last):
  File "foo.py", line 30, in 
    thread.join()
  File "…/gevent/greenlet.py", line 291, in join
    result = self.parent.switch()
  File "…/gevent/hub.py", line 381, in switch
    return greenlet.switch(self)
gevent.hub.LoopExit: This operation would block forever

However, Thread(…).start() (the second block), everything works as expected.

Why would this be? What’s the difference between gevent.spawn() and Thread(…).start()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:14:56+00:00Added an answer on June 13, 2026 at 10:14 am

    What happen in your code is that the greenlets that you have created in you threads list didn’t have yet the chance to be executed because gevent will not trigger a context switch until you do so explicitly in your code using gevent.sleep() and such or implicitly by calling a function that block e.g. semaphore.wait() or by yielding and so on …, to see that you can insert a print before cv.wait() and see that it’s called only after cv.notify_all() is called:

    def wait_on_cv(x):
        cv.acquire()
        print 'acquired ', x
        cv.wait()
        ....
    

    So an easy fix to your code will be to insert something that will trigger a context switch after you create your list of greenlets, example:

    ...
    threads = [ gevent.spawn(wait_on_cv, x) for x in range(10) ]
    gevent.sleep()  # Trigger a context switch
    ...
    

    Note: I am still new to gevent so i don’t know if this is the right way to do it 🙂

    This way all the greenlets will have the chance to be executed and each one of them will trigger a context switch when they call cv.wait() and in the mean time they will
    register them self to the condition waiters so that when cv.notify_all() is called it
    will notify all the greenlets.

    HTH,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does the following function perform? public static double CleanAngle(double angle) { while (angle
Do we need to do double buffering in BlackBerry while rendering our paint code
While answering this question, I got these confusing results: double d = 0.49999999999999990d; //output
I'm trying to verify that a character input to a double variable is of
I am getting a weird problem while parsing a double value in managed C++.
Recently I came across a situation where, while using a double-byte language, I had
I would like some advise on how to optimise the following while loop: double
While doing normal day-to-day Android development, is it safe to assume that hook methods
I have to handle some event while double tapping on a button how to
While building my CUDA project I get the following error: cutil_inline_runtime.h(328): error: identifier CURAND_STATUS_DOUBLE_PRECISION_REQUIRED

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.