##// END OF EJS Templates
Experimental patch to keep Trio event loop alive between cells...
Mark E. Haase -
Show More
@@ -42,9 +42,44 b' def _curio_runner(coroutine):'
42 return curio.run(coroutine)
42 return curio.run(coroutine)
43
43
44
44
45 _TRIO_TOKEN = None
46 _TRIO_NURSERY = None
47
48
49 def _init_trio(trio):
50 global _TRIO_TOKEN
51 import traceback
52 import builtins
53 import threading
54
55 async def trio_entry():
56 global _TRIO_TOKEN, _TRIO_NURSERY
57 _TRIO_TOKEN = trio.hazmat.current_trio_token()
58 async with trio.open_nursery() as nursery:
59 _TRIO_NURSERY = nursery
60 builtins.GLOBAL_NURSERY = nursery
61 await trio.sleep_forever()
62
63 def trio_entry_sync():
64 while True:
65 try:
66 trio.run(trio_entry)
67 except Exception:
68 print("Exception in trio event loop:", traceback.format_exc())
69
70 threading.Thread(target=trio_entry_sync).start()
71 #TODO fix this race condition
72 import time
73 while not _TRIO_TOKEN:
74 time.sleep(0.1)
75
76
45 def _trio_runner(async_fn):
77 def _trio_runner(async_fn):
46 import trio
78 import trio
47
79
80 if not _TRIO_TOKEN:
81 _init_trio(trio)
82
48 async def loc(coro):
83 async def loc(coro):
49 """
84 """
50 We need the dummy no-op async def to protect from
85 We need the dummy no-op async def to protect from
@@ -52,7 +87,7 b' def _trio_runner(async_fn):'
52 """
87 """
53 return await coro
88 return await coro
54
89
55 return trio.run(loc, async_fn)
90 return trio.from_thread.run(loc, async_fn, trio_token=_TRIO_TOKEN)
56
91
57
92
58 def _pseudo_sync_runner(coro):
93 def _pseudo_sync_runner(coro):
@@ -2865,7 +2865,7 b' class InteractiveShell(SingletonConfigurable):'
2865 # when this is the case, we want to run it using the pseudo_sync_runner
2865 # when this is the case, we want to run it using the pseudo_sync_runner
2866 # so that code can invoke eventloops (for example via the %run , and
2866 # so that code can invoke eventloops (for example via the %run , and
2867 # `%paste` magic.
2867 # `%paste` magic.
2868 if self.should_run_async(raw_cell):
2868 if self.should_run_async(raw_cell) or self.loop_runner is _trio_runner:
2869 runner = self.loop_runner
2869 runner = self.loop_runner
2870 else:
2870 else:
2871 runner = _pseudo_sync_runner
2871 runner = _pseudo_sync_runner
General Comments 0
You need to be logged in to leave comments. Login now