Gevent, sockets and synchronization

I have several potions sent to a shared socket. Is it guaranteed that every packet sent through socket.sendall is well separated or do I need to get a lock before every call to sendall .

Therefore, I want to prevent the following scenario:

  • g1 sends ABCD
  • g2 sends 1234
  • the received data is mixed, for example AB1234CD
  • expected to be either ABCD1234 or 1234ABCD

Update

Looking at the source code , I think that this scenario cannot be. But I have to use a lock because g1 or g2 may crash on sendall . Can someone confirm this?

+4
source share
1 answer

I did some tests with a high latency / low bandwidth interface and got the expected error.

This led (as expected) to the following error:

 AssertionError: This event is already used by another greenlet: (<Greenlet at 0x7f3e758722d0: <bound method socket.sendall of <socket at 0x7f3e7587719 0 fileno=8 sock=127.0.0.1:1234 peer=127.0.0.1:51042>>('11111111111111111111 11111111111111111111111111111)>, timeout('timed out',)) 

Here is a fixed test script with gevent.coros.RLock that does not give this error: https://gist.github.com/4249827/7f02f805331eda4091ae0b39dfea4b102cdba2fa

+2
source

All Articles