Python套接字等待 [英] Python socket wait

查看:52
本文介绍了Python套接字等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以告诉 python 等待它从服务器获得响应以继续运行.

I was wondering if there is a way I can tell python to wait until it gets a response from a server to continue running.

我正在编写一个回合制游戏.我进行了第一步,它将移动发送到服务器,然后将服务器发送到另一台计算机.问题来了.由于不再轮到我,我希望我的游戏等到它得到服务器的响应(等到其他玩家采取行动).但我的线路:

I am writing a turn based game. I make the first move and it sends the move to the server and then the server to the other computer. The problem comes here. As it is no longer my turn I want my game to wait until it gets a response from the server (wait until the other player makes a move). But my line:

data=self.sock.recv(1024)

挂起是因为(我认为)它没有立即得到一些东西.所以我想知道如何让它等待某些事情发生然后继续前进.

hangs because (I think) it's no getting something immediately. So I want know how can I make it wait for something to happen and then keep going.

提前致谢.

推荐答案

socket programming howto 与这个问题有关,特别是这部分:

The socket programming howto is relevant to this question, specifically this part:

现在我们来到了套接字的主要绊脚石——send 和 recv 操作网络缓冲区.它们不一定处理您交给它们的所有字节(或期望从他们),因为他们的主要重点是处理网络缓冲区.一般来说,他们当关联的网络缓冲区已填充(发送)或清空(接收)时返回.然后他们会告诉你他们处理了多少字节.你有责任给他们打电话直到您的消息完全处理完毕.

Now we come to the major stumbling block of sockets - send and recv operate on the network buffers. They do not necessarily handle all the bytes you hand them (or expect from them), because their major focus is handling the network buffers. In general, they return when the associated network buffers have been filled (send) or emptied (recv). They then tell you how many bytes they handled. It is your responsibility to call them again until your message has been completely dealt with.

...

需要注意的一个复杂问题:如果您的对话协议允许多个要背靠背发送的消息(没有某种回复),然后您通过 recv 和任意块大小,您最终可能会阅读以下消息的开头.你会需要把它放在一边>并坚持下去,直到需要它为止.

One complication to be aware of: if your conversational protocol allows multiple messages to be sent back to back (without some kind of reply), and you pass recv an arbitrary chunk size, you may end up reading the start of a following message. You’ll need to put that aside >and hold onto it, until it’s needed.

用它的长度(比如,作为 5 个数字字符)作为消息前缀变得更加复杂,因为(信不信由你),您可能无法在一次接收中获得所有 5 个字符.在玩周围,​​你会逃脱它;但是在高网络负载下,您的代码会很快除非您使用两个 recv 循环,否则中断 - 第一个确定长度,第二个确定获取消息的数据部分.讨厌.这也是您会发现发送并不总是能够一次性解决所有问题.尽管已经阅读这个,你最终会被它咬住的!

Prefixing the message with it’s length (say, as 5 numeric characters) gets more complex, because (believe it or not), you may not get all 5 characters in one recv. In playing around, you’ll get away with it; but in high network loads, your code will very quickly break unless you use two recv loops - the first to determine the length, the second to get the data part of the message. Nasty. This is also when you’ll discover that send does not always manage to get rid of everything in one pass. And despite having read this, you will eventually get bit by it!

主要结论是:

  • 您需要建立固定的消息大小,或者您需要在消息的开头发送消息的大小

  • you'll need to establish either a FIXED message size, OR you'll need to send the the size of the message at the beginning of the message

在调用 socket.recv 时,传递您实际想要的字节数(我猜您实际上并不想要 1024 字节).然后使用 LOOP,因为您不能保证在一次调用中获得您想要的所有内容.

when calling socket.recv, pass number of bytes you actually want (and I'm guessing you don't actually want 1024 bytes). Then use LOOPs because you are not guaranteed to get all you want in a single call.

这篇关于Python套接字等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆