Show More
@@ -43,7 +43,6 b' def _curio_runner(coroutine):' | |||
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | _TRIO_TOKEN = None |
|
46 | _TRIO_NURSERY = None | |
|
47 | 46 | |
|
48 | 47 | |
|
49 | 48 | def _init_trio(trio): |
@@ -51,30 +50,36 b' def _init_trio(trio):' | |||
|
51 | 50 | import traceback |
|
52 | 51 | import builtins |
|
53 | 52 | import threading |
|
53 | # We use an Event to avoid a race condition between starting the Trio thread | |
|
54 | # and running Trio code. | |
|
55 | thread_start = threading.Event() | |
|
56 | ||
|
57 | def log_nursery_exc(exc): | |
|
58 | import logging | |
|
59 | import traceback | |
|
60 | exc = '\n'.join(traceback.format_exception(type(exc), exc, | |
|
61 | exc.__traceback__)) | |
|
62 | logging.error('An exception occurred in a global nursery task.\n%s', | |
|
63 | exc) | |
|
54 | 64 | |
|
55 | 65 | async def trio_entry(): |
|
56 | 66 | global _TRIO_TOKEN, _TRIO_NURSERY |
|
57 | 67 | _TRIO_TOKEN = trio.hazmat.current_trio_token() |
|
58 | 68 | async with trio.open_nursery() as nursery: |
|
59 | _TRIO_NURSERY = nursery | |
|
69 | # TODO This hack prevents the nursery from cancelling all child | |
|
70 | # tasks when but it's ugly. | |
|
71 | nursery._add_exc = log_nursery_exc | |
|
60 | 72 | builtins.GLOBAL_NURSERY = nursery |
|
73 | thread_start.set() | |
|
61 | 74 | await trio.sleep_forever() |
|
62 | 75 | |
|
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) | |
|
76 | threading.Thread(target=trio.run, args=(trio_entry,)).start() | |
|
77 | thread_start.wait() | |
|
75 | 78 | |
|
76 | 79 | |
|
77 | 80 | def _trio_runner(async_fn): |
|
81 | global _TRIO_TOKEN | |
|
82 | import logging | |
|
78 | 83 | import trio |
|
79 | 84 | |
|
80 | 85 | if not _TRIO_TOKEN: |
General Comments 0
You need to be logged in to leave comments.
Login now