【问题标题】:How can I set up a Pusher server with Flask?如何使用 Flask 设置 Pusher 服务器?
【发布时间】:2020-10-14 23:27:42
【问题描述】:

我正在尝试设置一个简单的flask 服务器:

import envkey
import pysher
from flask import Flask
# from predictor import PythonPredictor
app = Flask(__name__)


pusher = pysher.Pusher(envkey.get('PUSHER_KEY'))


def my_func(*args, **kwargs):
    print("processing Args:", args)
    print("processing Kwargs:", kwargs)

# We can't subscribe until we've connected, so we use a callback handler
# to subscribe when able


def connect_handler(data):
    print('connect habndler')
    channel = pusher.subscribe('mychannel')
    channel.bind('myevent', my_func)


pusher.connection.bind('pusher:connection_established', connect_handler)


@app.route('/')
def index():

    pusher.connect()

    return 'Server Works!'

但我得到一个错误:

RuntimeError: cannot join current thread

我做错了什么?

【问题讨论】:

  • 好像已经有一个正在运行的进程并且没有被杀死....你能用ps -aux命令验证它,如果它已经在运行就杀死它吗?
  • 错误发生在哪一点?你什么时候启动你的 Flask 服务器,或者你什么时候用pusher.connect() 语句戳那个端点?
  • .connect 是它失败的地方

标签: python flask websocket pusher


【解决方案1】:

在初始化期间指定我的 Pusher 集群帮助我摆脱了这个问题:

pusher = pysher.Pusher(
    key=envkey.get('PUSHER_KEY'),  # Or however you get the key
    cluster="eu",  # Add cluster!
)

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 2020-05-01
    • 2019-05-23
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2016-10-04
    • 2020-12-18
    相关资源
    最近更新 更多