客户端脚本:
class TCPClient(object):
def __init__(self, counter, host, port, io_loop=None):
self.counter=counter
self.host = host
self.port = port
self.io_loop = io_loop
self.shutdown = False
self.stream = None
self.sock_fd = None
self.EOF = b' END'
def get_stream(self):
self.sock_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self.stream = tornado.iostream.IOStream(self.sock_fd)
self.stream.set_close_callback(self.on_close)
def connect(self):
self.get_stream()
self.stream.connect((self.host, self.port), self.send_message)
def on_receive(self, data):
logging.info("Received: %s", data)
self.stream.close()
def on_close(self):
if self.shutdown:
self.io_loop.stop()
def send_message(self):
mac_adresi = 123
my_json_string = json.dumps(OrderedDict(mac_adresi=mac_adresi, adet=self.counter))
self.stream.write(my_json_string)
self.stream.read_until(self.EOF, self.on_receive)
def set_shutdown(self):
self.shutdown = True
def main(counter, host, port, io_loop):
io_loop = tornado.ioloop.IOLoop.instance()
c1 = TCPClient(counter, host, port, io_loop)
c1.connect()
c1.set_shutdown()
io_loop.start()
反脚本:
import Client
counter = 0
while 1:
counter += 1
if counter%9 > 5:
Client.main(counter, "127.0.0.1", 51276)
问题是如果我从计数器脚本调用客户端脚本的 main ,客户端脚本执行一次并卡住在那里,无法返回反脚本。
我想在不阻止 Counter Script 的情况下发送请求,如果任何时候 counter 大于 5 。