gevent,套接字和同步

sch*_*mar 5 python sockets gevent

我在公共套接字上发送了多个greenlet。是否可以确保通过发送的每个程序包socket.sendall都分开良好,还是在每次调用之前我都必须获得一个锁sendall

因此,我想防止出现以下情况:

  • g1发送ABCD
  • g2发送1234
  • 例如,接收到的数据混合在一起 AB1234CD
  • 预期是ABCD12341234ABCD

更新资料

看完源代码后,我认为这种情况不会发生。但是我必须使用锁,因为g1或g2可能会在上崩溃sendall。有人可以确认吗?

sch*_*mar 2

我使用高延迟/低带宽接口进行了一些测试,并得到了预期的错误。

这导致(如预期)出现以下错误:

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',))
Run Code Online (Sandbox Code Playgroud)

这是固定的测试脚本gevent.coros.RLock,不会产生此错误:https://gist.github.com/4249827/7f02f805331eda4091ae0b39dfea4b102cdba2fa