Search code examples
geventapschedulerzerorpc

APScheduler and zerorpc incompatibility


It looks like that scheduling jobs with APScheduler doesn't go along with ZeroRPC.

  • When testing using test(x) ZeroRPC works well.
  • When starting with run() the scheduler repeatedly returns the following exception:
gevent.exceptions.LoopExit: This operation would block forever
    Hub: <Hub '' at 0x1074aff60 select pending=0 ref=0 thread_ident=0x70000c356000>
    Handles:
[]

Following, the code snippet:

mport zerorpc
from apscheduler.schedulers.background import BlockingScheduler
from datetime import datetime

rpc = zerorpc.Client()
rpc.connect("tcp://{}:{}".format('127.0.0.1', 4242))

def test(x):
    rpc.write('variable_name', x)

def run():
    def test():
        rpc.write('variable_name', 0)
        rpc.write('variable_name', 1)

    scheduler = BlockingScheduler()
    scheduler.add_job(
        func=test,
        trigger='interval',
        seconds=1,
        start_date=datetime.now(),
    )

    scheduler.start()

Solution

  • APScheduler creates new Threads when scheduling new tasks. ZeroRPC needs to have an instance for every opened Thread.