1
1
import trio
2
2
3
- def thread_fn (portal , receive_from_trio , send_to_trio ):
3
+
4
+ def thread_fn (receive_from_trio , send_to_trio ):
4
5
while True :
5
6
# Since we're in a thread, we can't call methods on Trio
6
- # objects directly -- so we use our portal to call them.
7
+ # objects directly -- so we use trio.from_thread to call them.
7
8
try :
8
- request = portal .run (receive_from_trio .receive )
9
+ request = trio . from_thread .run (receive_from_trio .receive )
9
10
except trio .EndOfChannel :
10
- portal .run (send_to_trio .aclose )
11
+ trio . from_thread .run (send_to_trio .aclose )
11
12
return
12
13
else :
13
14
response = request + 1
14
- portal .run (send_to_trio .send , response )
15
+ trio .from_thread .run (send_to_trio .send , response )
16
+
15
17
16
18
async def main ():
17
- portal = trio .BlockingTrioPortal ()
18
19
send_to_thread , receive_from_trio = trio .open_memory_channel (0 )
19
20
send_to_trio , receive_from_thread = trio .open_memory_channel (0 )
20
21
21
22
async with trio .open_nursery () as nursery :
22
23
# In a background thread, run:
23
24
# thread_fn(portal, receive_from_trio, send_to_trio)
24
25
nursery .start_soon (
25
- trio .run_sync_in_thread ,
26
- thread_fn , portal , receive_from_trio , send_to_trio
26
+ trio .run_sync_in_thread , thread_fn , receive_from_trio , send_to_trio
27
27
)
28
28
29
29
# prints "1"
@@ -40,4 +40,5 @@ async def main():
40
40
# When we exit the nursery, it waits for the background thread to
41
41
# exit.
42
42
43
+
43
44
trio .run (main )
0 commit comments