-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1551 from njsmith/guest-loop
"Guest mode", for cohabitation with Qt etc.
- Loading branch information
Showing
13 changed files
with
1,530 additions
and
241 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
If you want to use Trio, but are stuck with some other event loop like | ||
Qt or PyGame, then good news: now you can have both. For details, see: | ||
:ref:`guest-mode`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import asyncio | ||
import trio | ||
|
||
async def aio_main(): | ||
loop = asyncio.get_running_loop() | ||
|
||
trio_done_fut = asyncio.Future() | ||
def trio_done_callback(main_outcome): | ||
print(f"trio_main finished: {main_outcome!r}") | ||
trio_done_fut.set_result(main_outcome) | ||
|
||
trio.lowlevel.start_guest_run( | ||
trio_main, | ||
run_sync_soon_threadsafe=loop.call_soon_threadsafe, | ||
done_callback=trio_done_callback, | ||
) | ||
|
||
(await trio_done_fut).unwrap() | ||
|
||
|
||
async def trio_main(): | ||
print("trio_main!") | ||
|
||
to_trio, from_aio = trio.open_memory_channel(float("inf")) | ||
from_trio = asyncio.Queue() | ||
|
||
asyncio.create_task(aio_pingpong(from_trio, to_trio)) | ||
|
||
from_trio.put_nowait(0) | ||
|
||
async for n in from_aio: | ||
print(f"trio got: {n}") | ||
await trio.sleep(1) | ||
from_trio.put_nowait(n + 1) | ||
if n >= 10: | ||
return | ||
|
||
async def aio_pingpong(from_trio, to_trio): | ||
print("aio_pingpong!") | ||
|
||
while True: | ||
n = await from_trio.get() | ||
print(f"aio got: {n}") | ||
await asyncio.sleep(1) | ||
to_trio.send_nowait(n + 1) | ||
|
||
|
||
asyncio.run(aio_main()) |
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
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
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
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
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
Oops, something went wrong.