Sunday, September 8, 2024

Go vs Python Part 1

I want to be able to send and recv on a socket at the same time. 

Go has a nice feature where you can select on channels.

Python does not allow you to do this. 

In fact, Windows Python does not allow you to select on anything except sockets.

Fortunately since Python 3.5, socketpair is available.

Using socketpair you can do a hacky workaround: https://stackoverflow.com/questions/51104534/python-socket-receive-send-multi-threading

You create an extra socket and then you do select on that extra socket (for send) along with the "main" socket (for recv).

But since syscalls are expensive you only since a single byte on that socket to notify the other thread to process the queue.

Anyway I thought this was a pretty inelegant workaround for what is basically a missing language feature.

If Python allowed you to do select on queues like Go allows you to select on channels then this hacky workaround would not be required.

No comments:

Post a Comment