을 기다릴 변경하는 경우 1. 클라이언트가 서버에 연결하여 업데이트를 2. 서버 검사를 기다렸다가 클라이언트에 데이터를 못살게 굴지 하드 당신이 당신의 internet.XXXServer
또는 reactor.listenXXX
(또는 XXXXEndpoint
전화)에 대한 링크를 제공하는 조각은 머리 또는 꼬리 그것의,하지만 ...
우선, 정상 사용, 트위스트 protocol.Protocol
가의 dataReceived
을 만드는 방법 프레임 워크 자체에서만 호출됩니다. 클라이언트 또는 서버 연결에 직접 또는 공장을 통해 연결되며 데이터가 지정된 연결에 들어올 때 자동으로 호출됩니다. (대부분의 트위스티드 프로토콜과 인터페이스는 폴링/콜 레이터가 아닌 인터럽트 기반이기 때문에 Twisted가 CPU를 효율적으로 만드는 이유 중 일부)
따라서 표시된 코드가 실제로 Server
을 통해 Twisted로 연결되는 경우 또는 listen
또는 Endpoint
귀하의 고객에게 다음 나는 당신의 클라이언트가 이제까지 데이터를 보낼 경우 매우 나쁜 일이 일어날 것이라고 생각합니다. ... 왜곡 된 dataReceived
(다른 문제들 중 하나)은 reactor.callLater
콜백과 모든 종류의) 혼란의 ...)
대신 코드가 꼬인 연결 프레임 워크에 연결되어 있지 않은 경우, 다음을 위해 설계되지 않은 공간에서 꼬인 클래스를 재사용하려고 시도합니다 (... 수동으로 설정하지 않는 한 비 연결 코드가 전송 방법을 배우는 방법을 모르기 때문에 이것이 가능하지 않을 것으로 생각됩니다.)
이런 식으로 건물 모델을 구축 한 방법은 다음과 같습니다. 폴링 기반 I/O에 대해 완전히 별개의 클래스를 만들기 위해 인스턴스화 한 후에 내 클라이언트 목록 (서버) 팩토리를 폴링 인스턴스 (예 : mypollingthing.servfact = myserverfactory
)에 푸시합니다. 클라이언트에 전화 할 수 있습니다. (내 폴링 로직에 맞는 정확한 레벨로 추상화하기 위해 만든 def.)
예를 들어 Krondo의 Twisted Introduction에서 예제를 취하는 경향이 있습니다. 비틀어 짐 (다른 비틀린 행렬), 예 : part 6의 "클라이언트 3.0"PoetryClientFactory
에는 공장에서 콜백을 설정하는 __init__
이 있습니다. 나는 twistedmatrix chat example과 몇 가지 다른 것들로 그 혼합하려고하면
, 내가 얻을 : 이
#!/usr/bin/python
from twisted.internet import task
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ServerFactory
class PollingIOThingy(object):
def __init__(self):
self.sendingcallback = None # Note I'm pushing sendToAll into here in main
self.iotries = 0
def pollingtry(self):
self.iotries += 1
print "Polling runs: " + str(self.iotries)
if self.sendingcallback:
self.sendingcallback("Polling runs: " + str(self.iotries) + "\n")
class MyClientConnections(Protocol):
def connectionMade(self):
print "Got new client!"
self.factory.clients.append(self)
def connectionLost(self, reason):
print "Lost a client!"
self.factory.clients.remove(self)
class MyServerFactory(ServerFactory):
protocol = MyClientConnections
def __init__(self):
self.clients = []
def sendToAll(self, message):
for c in self.clients:
c.transport.write(message)
def main():
client_connection_factory = MyServerFactory()
polling_stuff = PollingIOThingy()
# the following line is what this example is all about:
polling_stuff.sendingcallback = client_connection_factory.sendToAll
# push the client connections send def into my polling class
# if you want to run something ever second (instead of 1 second after
# the end of your last code run, which could vary) do:
l = task.LoopingCall(polling_stuff.pollingtry)
l.start(1.0)
# from: https://twistedmatrix.com/documents/12.3.0/core/howto/time.html
reactor.listenTCP(5000, client_connection_factory)
reactor.run()
if __name__ == '__main__':
main()
공정하게 (당신은 당신의 self.orders_queue.has_new_orders()
에 대한 무엇이든에 sendToAll
을 변경할 수 있습니다), 그것은 수도 호출 코드를 __init__
(즉, Krondo의 문서에 나와있는 것)으로 전달하여 콜백을 PollingIOThingy
에게 알려주는 것이 더 나을 것입니다. 웬일인지, 코드를 읽고 클래스 - 속임수를 쉽게 찾을 수있는 이런 연결을 놓치는 경향이 있습니다. 보세요,하지만 그건 내 개인적인 뇌 손상일지도 모릅니다.
다른 것들은 따로하고, 정말로 정말로 이것과 같이'dataReceived'를 호출하면 안됩니다. 이 논리를 이미 (다른, 호환되지 않는) 의미를 가지고 있지 않은 방법에 넣는다면 코드는 잘 작동 할 것입니다. –