File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -122,9 +122,17 @@ Compatibility Mode
122122------------------
123123
124124You still can do things "the asyncio way": the to-be-replaced code from the
125- :ref: `previosu section <native-loop >`
126- still works – or at least it attempts to work.
125+ :ref: `previous section <native-loop >`
126+ still works – or at least it attempts to work::
127127
128+ import asyncio
129+ import trio_asyncio
130+ asyncio.set_event_loop_policy(trio_asyncio.TrioPolicy())
131+
132+ def main():
133+ loop = asyncio.get_event_loop()
134+ loop.run_until_complete(async_main())
135+
128136.. warning ::
129137
130138 tl;dr: Don't use Compatibility Mode in production code.
@@ -150,6 +158,11 @@ just as with a regular asyncio loop.
150158If you use a compatibility-mode loop in a separate thread, you *must * stop and close it
151159before terminating that thread. Otherwise your thread will leak resources.
152160
161+ In a multi-threaded program, globally setting the event loop policy may not
162+ be a good idea. If you want to run trio-asyncio in a separate thread, you
163+ might get away with using ``TrioPolicy().new_event_loop() `` to create a new
164+ event loop – but a far better idea is to use native mode.
165+
153166.. note ::
154167
155168 Compatibility mode has been added to verify that various test suites,
Original file line number Diff line number Diff line change @@ -106,11 +106,9 @@ def _main_loop_exit(self):
106106 self ._thread = None
107107
108108 async with trio .open_nursery () as nursery :
109- old_loop = asyncio .get_event_loop ()
110109 loop = TrioEventLoop (queue_len = queue_len )
111110 try :
112111 loop ._closed = False
113- asyncio .set_event_loop (loop )
114112 await loop ._main_loop_init (nursery )
115113 await nursery .start (loop ._main_loop )
116114 await yield_ (loop )
@@ -120,5 +118,4 @@ def _main_loop_exit(self):
120118 finally :
121119 await loop ._main_loop_exit ()
122120 loop .close ()
123- asyncio .set_event_loop (old_loop )
124121 nursery .cancel_scope .cancel ()
Original file line number Diff line number Diff line change @@ -646,6 +646,7 @@ async def _main_loop_init(self, nursery):
646646 self ._nursery = nursery
647647 self ._task = trio .hazmat .current_task ()
648648 self ._token = trio .hazmat .current_trio_token ()
649+ asyncio .events ._set_running_loop (self )
649650
650651 async def _main_loop (self , task_status = trio .TASK_STATUS_IGNORED ):
651652 """Run the loop by processing its event queue.
@@ -725,6 +726,7 @@ async def _main_loop_exit(self):
725726 # clean core fields
726727 self ._nursery = None
727728 self ._task = None
729+ asyncio .events ._set_running_loop (None )
728730
729731 def is_running (self ):
730732 if self ._stopped is None :
Original file line number Diff line number Diff line change @@ -98,7 +98,7 @@ def set_event_loop(self, loop):
9898 # in a sub-task, which is exactly what we intend to be possible
9999 if self ._trio_local ._loop is not None and loop is not None and \
100100 self ._trio_local ._task == task :
101- raise RuntimeError ('You cannot replace an event loop.' )
101+ raise RuntimeError ('You cannot replace an event loop.' , self . _trio_local . _loop , loop )
102102 self ._trio_local ._loop = loop
103103 self ._trio_local ._task = task
104104
@@ -231,5 +231,3 @@ async def _run_task(proc, args):
231231
232232 trio .run (_run_task , proc , args )
233233
234-
235- asyncio .set_event_loop_policy (TrioPolicy ())
You can’t perform that action at this time.
0 commit comments