[stacklessexamples] r183 committed - Add monkey-patching support for 'select'. This is because if we are m...

2 views
Skip to first unread message

stackles...@googlecode.com

unread,
Feb 4, 2011, 12:50:39 AM2/4/11
to stackless-exa...@googlegroups.com
Revision: 183
Author: richar...@gmail.com
Date: Thu Feb 3 21:50:02 2011
Log: Add monkey-patching support for 'select'. This is because if we are
making threads into tasklets then we need to offload actual thread-blocking
operations onto other real threads to prevent blocking the scheduler (and
whatever condition might unblock).
http://code.google.com/p/stacklessexamples/source/detail?r=183

Added:
/trunk/libraries/stacklesslib/replacements/select.py
Modified:
/trunk/libraries/stacklesslib/magic.py

=======================================
--- /dev/null
+++ /trunk/libraries/stacklesslib/replacements/select.py Thu Feb 3
21:50:02 2011
@@ -0,0 +1,26 @@
+# If we are using making threads into tasklets and other fancy
+# mucking with the natural order of things, then we need to ensure
+# that blocking operations do not block the thread a tasklet is
+# running on. Otherwise, they block the scheduler that tasklet
+# is running within. To this end, we make blocking calls to select
+# actually get delegated to another REAL thread.
+
+from __future__ import absolute_import
+
+import stackless
+import stacklesslib.util
+
+import select as real_select
+
+error = real_select.error
+__doc__ = real_select.__doc__
+
+def select(*args, **kwargs):
+ # If it blocks until it gets a result, or for longer than a nominal
+ # amount, then farm it off onto another thread.
+ if len(args) == 3 or len(args) == 4 and args[3] > 0.01 or "timeout" in
kwargs and kwargs["timeout"] > 0.01:
+ return stacklesslib.util.call_on_thread(real_select.select, args,
kwargs)
+ # Otherwise, do it inline and expect to return effectively immediately.
+ return real_select.select(*args)
+
+select.__doc__ = real_select.select.__doc__
=======================================
--- /trunk/libraries/stacklesslib/magic.py Sun Dec 26 17:09:57 2010
+++ /trunk/libraries/stacklesslib/magic.py Thu Feb 3 21:50:02 2011
@@ -13,7 +13,7 @@
except:
stacklessio = False

-from stacklesslib.replacements import thread, threading, popen
+from stacklesslib.replacements import thread, threading, popen, select

def monkeypatch():
#inject stacklessio
@@ -23,6 +23,7 @@
#inject slthreading as threading
sys.modules["threading"] = threading
sys.modules["thread"] = thread
+ sys.modules["select"] = select

#fudge time.sleep
time.sleep = main.sleep

Reply all
Reply to author
Forward
0 new messages