socket python : recvfrom

北慕城南 提交于 2019-12-10 18:43:12

问题


I would like to know if socket.recvfrom in python is a blocking function ? I couldn't find my answer in the documentation If it isn't, what will be return if nothing is receive ? An empty string '' ? In the other case, if in fact, it is blocking, how can i do to put it as an unblocking function ? I heard about settimeout but I don't know if it is actually the right solution.


回答1:


By default it is blocking. It can be turned into non-blocking via socket.setblocking(0) or (equivalently) socket.settimeout(0). In that case if there is nothing to receive it will throw socket.error exception. See the docs: https://docs.python.org/2/library/socket.html#socket.socket.setblocking



来源:https://stackoverflow.com/questions/31166594/socket-python-recvfrom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!