[Jython-checkins] jython: time.sleep(0) should always attempt to yield

jim.baker jython-checkins at python.org
Sat Mar 7 15:45:53 CET 2015


https://hg.python.org/jython/rev/d404d911f3e9
changeset:   7603:d404d911f3e9
user:        Jim Baker <jim.baker at rackspace.com>
date:        Fri Mar 06 23:55:26 2015 -0700
summary:
  time.sleep(0) should always attempt to yield

Use java.lang.Thread.yield() instead of j.l.Thread.sleep when the
sleep time is zero, to conform better with CPython's scheduling
semantics.

files:
  src/org/python/modules/time/Time.java |  19 ++++++++++----
  1 files changed, 13 insertions(+), 6 deletions(-)


diff --git a/src/org/python/modules/time/Time.java b/src/org/python/modules/time/Time.java
--- a/src/org/python/modules/time/Time.java
+++ b/src/org/python/modules/time/Time.java
@@ -449,12 +449,19 @@
     }
 
     public static void sleep(double secs) {
-        try {
-            java.lang.Thread.sleep((long)(secs * 1000));
-        }
-        catch (java.lang.InterruptedException e) {
-            throw new PyException(Py.KeyboardInterrupt, "interrupted sleep");
-        }
+	if (secs == 0) {
+	    // Conform to undocumented, or at least very underdocumented, but quite
+	    // reasonable behavior in CPython. See Alex Martelli's answer,
+	    // http://stackoverflow.com/a/790246/423006
+	    java.lang.Thread.yield();
+	} else {
+	    try {
+		java.lang.Thread.sleep((long)(secs * 1000));
+	    }
+	    catch (java.lang.InterruptedException e) {
+		throw new PyException(Py.KeyboardInterrupt, "interrupted sleep");
+	    }
+	}
     }
 
     // set by classDictInit()

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list