Last active
April 6, 2016 09:44
-
-
Save tehasdf/9237b4271960af4335f89a51af505568 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.internet.protocol import Factory | |
class MyFactory(Factory): | |
protocol = MyProtocol | |
def __init__(self, queue): | |
self._queue = queue | |
def buildProtocol(self, addr): | |
return self.protocol(self._queue) | |
class MyProtocol(Protocol): | |
def __init__(self, queue): | |
self._queue = queue | |
def connectionMade(self): | |
self._wait_for_queue() | |
@inlineCallbacks | |
def _wait_for_queue(self): | |
'''this function is a thread which sends data to clients as Queue is filled by the on_move_up function''' | |
while True: | |
data = yield self._queue.get() | |
json_data=json.dumps(data) | |
self.transport.write(json_data) | |
class SomethingElse(object): | |
def __init__(self, queue): | |
self._queue = queue | |
def on_touch_up(self, touch): | |
if self.lines: | |
self._queue.put(self.lines) | |
line_queue = DeferredQueue() | |
reactor.connectTCP(host, port, MyFactory(line_queue)) | |
SomethingElse(line_queue) | |
# OR INSTEAD | |
# or btw you dont need a factory: | |
from twisted.internet.endpoints import connectProtocol, TCP4ClientEndpoint | |
ep = TCP4ClientEndpoint(reactor, host, port) | |
proto = MyProtocol(line_queue) # constructing a protocol directly, without a factory | |
connectProtocol(ep, proto) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment