2

I am trying to use the flag 'MSG_DONTWAIT' in the following python code:

RECV_BUFFER_SIZE = 1024
buff = memoryview(bytearray(RECV_BUFFER_SIZE))
x = client_socket.recv_into(buff, RECV_BUFFER_SIZE, socket.MSG_DONTWAIT)

where I am just reading from the socket.

Strangely, when I use 'MSG_WAITALL' flag, the code works fine, however it fails with the following error:

x = client_socket.recv_into(buff, RECV_BUFFER_SIZE, socket.MSG_DONTWAIT)

AttributeError: module 'socket' has no attribute 'MSG_DONTWAIT'

when I try to use 'MSG_DONTWAIT' from the same class MsgFlag in which 'MSG_WAITALL' was present. I am running this on windows platform.

bgse
  • 8,237
  • 2
  • 37
  • 39
Akay
  • 1,092
  • 12
  • 32
  • Might be platform specific, are you on Windows by chance? – bgse Jan 07 '19 at 09:26
  • yup, running this on windows! – Akay Jan 07 '19 at 09:29
  • Might be a version issue of `fluent` . https://github.com/fluent/fluent-logger-python/issues/129 – Uvar Jan 07 '19 at 10:09
  • how do I update this on my system? I remember I just installed the latest version of python on my system 2 days back! – Akay Jan 07 '19 at 10:21
  • As @bgse assumed, it is platform specific. WinSock does not implement that constant, hence it is not available in Python's socket package on a Windows installation. – shmee Jan 07 '19 at 10:29

1 Answers1

3

You receive the error because MSG_DONTWAIT is not defined in the Python socket module for Windows. Python only acts is as a tiny wrapper around the low level socket system, and it is Winsock2 on Windows. And specifically MSG_DONTWAIT seems to not be defined by Posix and does not exist in Winsock. On a Unix or BSD system, it would be defined in the Python socket module, I have just controlled on a FreeBSD 10 system.

According to this other SO question, and after reading the related post, I think that you have to explicitely put the socket in non blocking mode on Windows:

RECV_BUFFER_SIZE = 1024
buff = memoryview(bytearray(RECV_BUFFER_SIZE))
client_socket.setblocking(0)                   # put socket in non blocking mode
try:                                           # a BlockingIOError is raised if nothing is available
    x = client_socket.recv_into(buff, RECV_BUFFER_SIZE, 0)
except BlockingIOError:
    x = 0
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thanks for introducing me to the setblocking() function, I am now using a combination of client_socket.setblocking(True) and client_socket.settimeout(3) to wait for 3 seconds until we receive anything from the sever! – Akay Jan 07 '19 at 11:42