I'm using APScheduler in my application to schedule jobs, and python-socket.io to communicate with a browser client to notify it of jobs being executed. However, I'm finding that the socket.io client (regular JavaScript client) doesn't accept events being sent from a scheduled job.
Here's some partial code below.
# 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])
(...)
In another script, I'm calling it like this:
# 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()
is a method that does some things and then calls python-socket.io
to emit a message saying it has ended.
The socket.io server is configured elsewhere, is perfectly functional, and imported as sio
. I can send messages fine, except for when I try to do it from a method that has been scheduled as a job.
python-engine.io
appears to send the message from the logs, but on the client side, not only is the message never received, the client proceeds to timeout. This only occurs after it receives a message from a scheduled job, as before that I am still able to send messages (via regular method calls) successfully.
Server log:
[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
Client log:
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
Any assistance with this issue would be greatly appreciated.