Python websockets, subscribe to multiple channels

别来无恙 提交于 2019-12-04 20:32:59

Use partial to pass in additional arguments

from functools import partial

def on_open(ws, channel_name):
    """
    Notice the channel_name parameter
    """

# create a new function with the predefined variable
chan1 = partial(on_open, channel_name='channel 1')
# set the new function as the on_open callback
ws1.on_open = chan1

# do the same for the rest
chan2 = partial(on_open, channel_name='channel 2')
ws2.on_open = chan2

As a side note, consider using Tornado or Crossbar.io (aka autobahn). Those are proper asynchronous frameworks and make websocket development simpler as opposed to threads and multiprocessing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!