You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Good question! Trio doesn't currently have any "high level abstraction" for working with UDP, because UDP is pretty low-level :-). But you can use UDP with trio by using the "low level" module trio.socket. There are a lot of examples in the internet showing you how to use UDP in regular python using the socket module. It should be pretty straightforward to convert those to work with trio instead. I'll show you how.
The trio.socket module is a copy of the regular socket module, except that you have to put await in front of I/O operations. (And then, Python's normal rules say that you have to put any await inside an async def; this part is discussed in the trio tutorial.)
put the socket code inside an async def, and run it with trio.run
replace all references to the socket module with trio.socket
add await in front of methods like sock.sendto
And here's the server code from that page adapted to trio as well. If you run this, and then run the first program in a different terminal, you should see that it successfully sends and receives a message. (Unless I messed something up... I'm typing this on my phone so I can't test them myself!)
Glad that helped! If you have more questions feel free to ask here or in chat. Or even if you don't have a question, we'd love to hear more about what you're working on :-)
Activity
njsmith commentedon May 20, 2018
Good question! Trio doesn't currently have any "high level abstraction" for working with UDP, because UDP is pretty low-level :-). But you can use UDP with trio by using the "low level" module
trio.socket. There are a lot of examples in the internet showing you how to use UDP in regular python using thesocketmodule. It should be pretty straightforward to convert those to work with trio instead. I'll show you how.The
trio.socketmodule is a copy of the regularsocketmodule, except that you have to putawaitin front of I/O operations. (And then, Python's normal rules say that you have to put anyawaitinside anasync def; this part is discussed in the trio tutorial.)For example, here's some regular python code from https://wiki.python.org/moin/UdpCommunication (I added parentheses to the
printcalls to make it python 3 compatible):And here's the same code, but adapted for trio:
So the differences are:
import trioinstead ofimport socket. (Importingtrioautomatically importstrio.socket)async def, and run it withtrio.runsocketmodule withtrio.socketawaitin front of methods likesock.sendtoAnd here's the server code from that page adapted to trio as well. If you run this, and then run the first program in a different terminal, you should see that it successfully sends and receives a message. (Unless I messed something up... I'm typing this on my phone so I can't test them myself!)
njsmith commentedon May 21, 2018
Here's a more detailed example of using UDP with Trio: https://github.com/python-trio/trio/blob/master/notes-to-self/ntp-example.py
yanzixiang commentedon May 21, 2018
Thanks very much.
njsmith commentedon May 21, 2018
Glad that helped! If you have more questions feel free to ask here or in chat. Or even if you don't have a question, we'd love to hear more about what you're working on :-)