Show More
@@ -43,7 +43,6 b' def _curio_runner(coroutine):' | |||||
43 |
|
43 | |||
44 |
|
44 | |||
45 | _TRIO_TOKEN = None |
|
45 | _TRIO_TOKEN = None | |
46 | _TRIO_NURSERY = None |
|
|||
47 |
|
46 | |||
48 |
|
47 | |||
49 | def _init_trio(trio): |
|
48 | def _init_trio(trio): | |
@@ -51,30 +50,36 b' def _init_trio(trio):' | |||||
51 | import traceback |
|
50 | import traceback | |
52 | import builtins |
|
51 | import builtins | |
53 | import threading |
|
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 | async def trio_entry(): |
|
65 | async def trio_entry(): | |
56 | global _TRIO_TOKEN, _TRIO_NURSERY |
|
66 | global _TRIO_TOKEN, _TRIO_NURSERY | |
57 | _TRIO_TOKEN = trio.hazmat.current_trio_token() |
|
67 | _TRIO_TOKEN = trio.hazmat.current_trio_token() | |
58 | async with trio.open_nursery() as nursery: |
|
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 | builtins.GLOBAL_NURSERY = nursery |
|
72 | builtins.GLOBAL_NURSERY = nursery | |
|
73 | thread_start.set() | |||
61 | await trio.sleep_forever() |
|
74 | await trio.sleep_forever() | |
62 |
|
75 | |||
63 | def trio_entry_sync(): |
|
76 | threading.Thread(target=trio.run, args=(trio_entry,)).start() | |
64 | while True: |
|
77 | thread_start.wait() | |
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 |
|
78 | |||
76 |
|
79 | |||
77 | def _trio_runner(async_fn): |
|
80 | def _trio_runner(async_fn): | |
|
81 | global _TRIO_TOKEN | |||
|
82 | import logging | |||
78 | import trio |
|
83 | import trio | |
79 |
|
84 | |||
80 | if not _TRIO_TOKEN: |
|
85 | if not _TRIO_TOKEN: |
General Comments 0
You need to be logged in to leave comments.
Login now