Last active
December 21, 2015 21:08
-
-
Save flub/6365845 to your computer and use it in GitHub Desktop.
greenlet solaris crasher Upon interrupting this (^C) this crashes reliably: $ pybin/bin/python test2.py ^CException in thread Thread-14: Traceback (most recent call last): File "/export/home/flub/issue546/greenlet-issue14/pybin/lib/python2.7/threading.py", line 551, in __bootstrap_inner self.run() File "test2.py", line 23, in run gt.switch() Sys…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import threading | |
import time | |
import greenlet | |
sys.setcheckinterval(0) | |
class HubThread(threading.Thread): | |
"""A thread running multiple greenlets and switching between them""" | |
def run(self): | |
self.main_gt = greenlet.getcurrent() | |
while True: | |
coros = set() | |
for i in range(42): | |
coros.add(greenlet.greenlet(self.function_a)) | |
coros.add(greenlet.greenlet(self.function_b)) | |
while coros: | |
gt = coros.pop() | |
gt.switch() | |
if gt: | |
coros.add(gt) | |
def function_a(self, n=5): | |
"""A function just to have some frames on the stack""" | |
msg = 'A variable allocated on the stack' | |
# if n == 5: | |
# print 'a', | |
if (n % 2) == 0: | |
self.main_gt.switch() | |
if n > 0: | |
return self.function_b(n - 1) | |
else: | |
return msg | |
def function_b(self, n=5): | |
"""A second function to have some frames on the stack | |
We use 2 functions to not have a purely recursive stack. | |
""" | |
msg = 'A variable allocated on the stack' | |
# if n == 5: | |
# print 'b', | |
if (n % 2) == 0: | |
self.main_gt.switch() | |
if n > 0: | |
return self.function_a(n - 1) | |
else: | |
return msg | |
def main(): | |
threads = set() | |
for i in range(42): | |
t = HubThread() | |
t.daemon = True | |
threads.add(t) | |
for t in threads: | |
t.start() | |
while True: | |
time.sleep(100) | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
print '%s running threads' % len(threading.enumerate()) | |
print 'the end' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment