Python 将控制权返还给运输部门
我试图模拟服务器定期接收数据的情况。在我的设置中,我运行一个进程来设置服务器,另一个进程来设置一组客户端(只需考虑一个客户端)。我已经设置了一些代码,主要是通过将来自。服务器/客户端通过使用transport.write发送消息进行通信。首先,服务器告诉客户机启动(这很好)。客户端在取得进展时向服务器报告。让我困惑的是,当客户端完成时,我只会在最后收到这些断断续续的消息。这可能是缓冲区刷新的问题,我尝试了(未成功)类似的方法。而且,每条消息都非常大,我多次尝试发送相同的消息,以便清除缓冲区 我怀疑我看到的是一个问题,但我不知道如何消除它 非常感谢您在这方面或任何其他问题上提供的任何帮助 服务器:Python 将控制权返还给运输部门,python,twisted,twisted.internet,Python,Twisted,Twisted.internet,我试图模拟服务器定期接收数据的情况。在我的设置中,我运行一个进程来设置服务器,另一个进程来设置一组客户端(只需考虑一个客户端)。我已经设置了一些代码,主要是通过将来自。服务器/客户端通过使用transport.write发送消息进行通信。首先,服务器告诉客户机启动(这很好)。客户端在取得进展时向服务器报告。让我困惑的是,当客户端完成时,我只会在最后收到这些断断续续的消息。这可能是缓冲区刷新的问题,我尝试了(未成功)类似的方法。而且,每条消息都非常大,我多次尝试发送相同的消息,以便清除缓冲区 我怀
from twisted.internet import reactor, protocol
import time
import serverSideAnalysis
import pdb
#import bson, json, msgpack
import _pickle as pickle # I expect the users to authenticate and not
# do anything malicious.
PORT = 9000
NUM = 1
local_scratch="/local/scratch"
class Hub(protocol.Protocol):
def __init__(self,factory, clients, nclients):
self.clients = clients
self.nclients = nclients
self.factory = factory
self.dispatcher = serverSideAnalysis.ServerTalker(NUM, self,
local_scratch)
def connectionMade(self):
print("connected to user" , (self))
if len(self.clients) < self.nclients:
self.factory.clients.append(self)
else:
self.factory.clients[self.nclients] = self
if len(self.clients) == NUM:
val = input("Looks like everyone is here, shall we start? (Y/N)")
while (val.upper() != "Y"):
time.sleep(20)
val = input("Looks like everyone is here, shall we start??? (Y/N)")
message = pickle.dumps({"TASK": "INIT", "SUBTASK":"STORE"})
self.message(message) # This reaches the client as I had expected
def message(self, command):
for c in self.factory.clients:
c.transport.write(command)
def connectionLost(self, reason):
self.factory.clients.remove(self)
self.nclients -= 1
def dataReceived(self, data):
if len(self.clients) == NUM:
self.dispatcher.dispatch(data)
class PauseTransport(protocol.Protocol):
def makeConnection(self, transport):
transport.pauseProducing()
class HubFactory(protocol.Factory):
def __init__(self, num):
self.clients = set([])
self.nclients = 0
self.totConnections = num
def buildProtocol(self, addr):
print(self.nclients)
if self.nclients < self.totConnections:
self.nclients += 1
return Hub(self, self.clients, self.nclients)
protocol = PauseTransport()
protocol.factory = self
return protocol
factory = HubFactory(NUM)
reactor.listenTCP(PORT, factory)
factory.clients = []
reactor.run()
关于调度员所做工作的最后一点相关信息
在这两种情况下,它们加载已到达的消息(字典),并根据内容进行一些计算。每隔一段时间,他们使用消息
方法与他们的当前值进行通信
最后,我使用的是python 3.6。扭曲了18.9.0从协议返回控制到反应堆的方式。dataReceived方法是从该方法返回。例如:
def dataReceived(self, data):
self.cdispatcher.dispatch(data)
print("1 sent")
如果你想在这之后做更多的工作,你有一些选择。如果希望在经过一段时间后进行此项工作,请使用
reactor.callLater
。如果希望在将工作分派到另一个线程后执行该工作,请使用twisted.internet.threads.deferToThread
。如果您希望工作响应其他事件(例如,正在接收的数据),请将其放入处理该事件的回调中(例如,dataReceived
)。谢谢您的建议。最后,我在客户端使用deferToThread
调度任务,在服务器端,我设置了deferredQueue
来捕获传入消息并处理它们(如本文所建议的)。
def dataReceived(self, data):
self.cdispatcher.dispatch(data)
print("1 sent")