从APScheduler作业发送python-socket.io消息会导致客户端挂起

时间:2016-11-11 09:35:41

标签: python socket.io apscheduler

我在我的应用程序中使用APScheduler来调度作业,并且python-socket.io与浏览器客户端通信以通知它正在执行的作业。但是,我发现socket.io客户端(常规JavaScript客户端)不接受从预定作业发送的事件。

以下是部分代码。

# scheduler.py

sched = BackgroundScheduler() # with proper configuration and stuff
sched.start()

(...)

def add_job(method, param, time):
    start_time = datetime.now() + timedelta(seconds=time)
    trigger = DateTrigger(run_date=start_time)
    return sched.add_job(method, trigger=trigger, args=[param])

(...)

在另一个脚本中,我这样称呼它:

# otherscript.py

import scheduler

(...)

def terminate(name):
    # Do some stuff
    sio.emit('ended', name)

def schedule_kill(name, time_offset):
    # Do some stuff
    scheduler.add_job(terminate, name, time_offset)

(...)

terminate()是一种执行某些操作的方法,然后调用python-socket.io发出一条消息,说明它已经结束。

socket.io服务器在其他地方配置,功能完善,导入为sio。我可以发送消息,除非我尝试从已安排为作业的方法执行此操作。

python-engine.io似乎从日志中发送消息,但在客户端,不仅消息从未收到,客户端也会进入超时状态。这只发生在从预定作业收到消息之后,就像之前我仍然能够成功发送消息(通过常规方法调用)。

服务器日志:

[17:24:39] socketio  : emitting event "started" to all [/]
[17:24:39] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet MESSAGE data 2["started","my_data"]
[17:24:39] scheduler : Scheduled terminate in 10 sec (at 2016-11-11 17:24:49.189081)
[17:24:42] socketio  : emitting event "started" to all [/]
[17:24:42] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet MESSAGE data 2["started","test"]
[17:24:42] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Received packet PING data None
[17:24:42] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet PONG data None
[17:24:43] socketio  : emitting event "ended" to all [/]
[17:24:43] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet MESSAGE data 2["ended","test"]
[17:24:49] socketio  : emitting event "ended" to all [/]
[17:24:49] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet MESSAGE data 2["ended","my_data"]
[17:24:51] socketio  : emitting event "started" to all [/]
[17:24:51] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet MESSAGE data 2["started","test"]
[17:24:52] socketio  : emitting event "ended" to all [/]
[17:24:52] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet MESSAGE data 2["ended","test"]
[17:25:07] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Received packet PING data None
[17:25:07] engineio  : 5ab9e719cb2c492db2f46dfbd8385a55: Sending packet PONG data None

客户日志:

engine.io-client:socket socket receive: type "message", data "2["started","my_data"]" +21s
engine.io-client:socket socket receive: type "message", data "2["started","test"]" +3s
engine.io-client:socket writing ping packet - expecting pong within 60000ms +524ms
engine.io-client:socket flushing 1 packets in socket +1ms
engine.io-client:socket socket receive: type "pong", data "undefined" +1ms
engine.io-client:socket socket receive: type "message", data "2["ended","test"]" +284ms
engine.io-client:socket writing ping packet - expecting pong within 60000ms +25s
engine.io-client:socket flushing 1 packets in socket +0ms
engine.io-client:socket socket close with reason: "ping timeout" +1m

非常感谢您对此问题的任何帮助。

1 个答案:

答案 0 :(得分:0)

我在使用This type of cancer is also called upper tract urothelial cancer(UTUC)flask-socketio时遇到了问题,我使用APJobScheduler作为eventlet的线程库。猴子修补事件解决了我的大部分问题。

As Miguel said,可能是由于与flask-socketiosocketio使用的线程库不兼容。