-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (79 loc) · 2.6 KB
/
app.py
File metadata and controls
97 lines (79 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#app.py
"""
This module contains a collection of classes and functions that are generally
swapped out to a different implementation if an application is running under
the stackless-framework. It is intended for things such as sleep(), whose
Stackless-implementation won't work unless the framework is being ticket.
Contrast this with stacklesslib.locks.Lock() which also works as a normal
thread locking primitive.
The replacement is indirected, so that client code can bind directly
to the functions here, e.g. use "from stacklesslib.app import sleep"
"""
import time
import threading
from . import events
from .base import atomic, SignalChannel
class _SleepHandler(object):
"""
A class to support sleep functionality
"""
def __init__(self):
# a common channel for zero sleeps
self.chan = SignalChannel()
def sleep(self, delay):
if delay <= 0:
c = self.chan
else:
c = SignalChannel()
if delay <= 0:
_event_queue.call_soon(c.signal)
else:
_event_queue.call_later(delay, c.signal)
c.receive()
class _ObjProxy(object):
def __init__(self, name):
self._name = name
def __getattr__(self, attr):
return getattr(globals()[self._name], attr)
def sleep(delay):
_sleep(delay)
def Event():
return _Event()
def Lock():
return _Lock()
def RLock():
return _Rlock()
def Condition():
return _Condition()
def Semaphore():
return _Semaphore()
event_queue = _ObjProxy("_event_queue")
def install_vanilla():
"""
Set up the globals to use default thread-blocking features
"""
g = globals()
g["_sleep"] = time.sleep
g["_Event"] = threading.Event
g["_Lock"] = threading.Lock
g["_Rlock"] = threading.RLock
g["_Condition"] = threading.Condition
g["_Semaphore"] = threading.Semaphore
g["_event_queue"] = events.DummyEventQueue() # Use the dummy instance which raises an error
def install_stackless():
"""
Set up the globals for a functioning event event loop
"""
# import those here, to avoid circular dependencies at import time.
from . import locks
from . import main
g = globals()
g["_sleep"] = _SleepHandler().sleep
g["_Event"] = locks.Event
g["_Lock"] = locks.Lock
g["_Rlock"] = locks.RLock
g["_Condition"] = locks.Condition
g["_Semaphore"] = locks.Semaphore
g["_event_queue"] = main.event_queue # use the instance from the main
# Run in non-stackless mode until told differently
install_vanilla()