在python中通過單個套接字多次發送數據

[英]Sending data multiple times over a single socket in python


I have a python program where I use a server socket to send data. There is a class which has some Threading methods. Each method checks a queue and if the queue is not empty, it sends the data over the sever socket. Queues are being filled with what clients send to server(server is listening for input requests). Sending is accomplished with a method call:

我有一個python程序,我使用服務器套接字發送數據。有一個類有一些線程方法。每個方法都檢查一個隊列,如果隊列不為空,它會通過服務器套接字發送數據。隊列正在填充客戶端發送給服務器的內容(服務器正在偵聽輸入請求)。通過方法調用完成發送:

def send(self, data):
    self.sqn += 1
    try:
        self.clisock.send(data)
    except Exception, e:
        print 'Send packet failed with error: ' + e.message

When the program starts, sending rate is around 500, but after a while it decreases instantly to 30 with this exception:

當程序啟動時,發送速率大約為500,但過了一段時間后,它會立即減少到30,但有以下異常:

Send packet failed with error: <class 'socket.error'>>>[Errno 32] Broken pipe 

I don't know what causes the rate to increase! Any idea?

我不知道是什么原因導致率上升!任何的想法?

1 个解决方案

#1


That error is from your send function trying to write to a socket closed on the other side. If that is intended then catch the exception using

該錯誤來自您的發送功能,試圖寫入另一側關閉的套接字。如果是這樣,那么使用捕獲異常

import errno, socket
try:
    self.clisock.send(data)
except socket.error, err:
    if err[0] == errno.EPIPE:
        # do something
    else:
        pass # do something else

If this isn't intended behavior on the part of the client then you'll have to update your post with the corresponding client code.

如果這不是客戶端的預期行為,那么您將不得不使用相應的客戶端代碼更新您的帖子。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/10/72527234f538bbaa6750cc4f5c92c621.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com