我们可以启动多个客户端吗?龙卷风客户端?

发布于 2025-01-14 18:52:03 字数 617 浏览 2 评论 0原文

class DetectClient(object):
def __init__(self, url):
    self.url = url
    self.ws = None
    self.connect()
    PeriodicCallback(self.heartbeat, 3000).start()
    IOLoop.instance().start()

@gen.coroutine
def connect(self):
    try:
        self.ws = yield websocket_connect(self.url)
    except Exception as e:
        print("connection error, %s" % e)
    else:
        print("detect connected.")
        self.run()

似乎由于某种原因只能启动一个客户端实例。 像这样,如何在main函数中启动该客户端的两个实例?

if __name__ == "__main__":
    DetectClient('ws.//1231231')
    DetectClient('ws.//1231231')
class DetectClient(object):
def __init__(self, url):
    self.url = url
    self.ws = None
    self.connect()
    PeriodicCallback(self.heartbeat, 3000).start()
    IOLoop.instance().start()

@gen.coroutine
def connect(self):
    try:
        self.ws = yield websocket_connect(self.url)
    except Exception as e:
        print("connection error, %s" % e)
    else:
        print("detect connected.")
        self.run()

it seems only one client instance can be started by some reason.
like this, how to start two instances of this client in the main function?

if __name__ == "__main__":
    DetectClient('ws.//1231231')
    DetectClient('ws.//1231231')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-01-21 18:52:03

不要在每个客户端中启动 IOLoop。您只需全局启动 IOLoop 一次。

要同时运行多个协程,您可以使用 gen。 multi

这是一个修改后的代码示例(我没有测试过):

from tornado import gen, ioloop

class DetectClient(object):
    def __init__(self, url):
        self.url = url
        self.ws = None

    @gen.coroutine
    def connect(self):
        try:
            self.ws = yield websocket_connect(self.url)
        except Exception as e:
            print("connection error, %s" % e)
        else:
            print("detect connected.")
            self.run()

        PeriodicCallback(self.heartbeat, 3000).start()


@gen.coroutine
def main():
    waiter = gen.WaitIterator(
        DetectClient('ws.//1231231').connect(),
        DetectClient('ws.//1231231').connect()
    )

    while not waiter.done():
        try:
            yield waiter.next()
        except Exception as e:
            print(e)
            continue

if __name__ == '__main__':
    loop = ioloop.IOLoop.current()
    loop.run_sync(main)

Don't start IOLoop in every client. You only have to start the IOLoop once globally.

For running multiple coroutines simultaneously, you can use gen.multi:

Here's a modified code example (I've not tested it):

from tornado import gen, ioloop

class DetectClient(object):
    def __init__(self, url):
        self.url = url
        self.ws = None

    @gen.coroutine
    def connect(self):
        try:
            self.ws = yield websocket_connect(self.url)
        except Exception as e:
            print("connection error, %s" % e)
        else:
            print("detect connected.")
            self.run()

        PeriodicCallback(self.heartbeat, 3000).start()


@gen.coroutine
def main():
    waiter = gen.WaitIterator(
        DetectClient('ws.//1231231').connect(),
        DetectClient('ws.//1231231').connect()
    )

    while not waiter.done():
        try:
            yield waiter.next()
        except Exception as e:
            print(e)
            continue

if __name__ == '__main__':
    loop = ioloop.IOLoop.current()
    loop.run_sync(main)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文