I have a use case where I need to run a couple of cpu intensive jobs on regular intervals so I'd like to have each job run in its own process instead of a thread. I'm using Python 2.7, so I've installed
futures. Just as a test, I've created a simple script:
import datetime
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from apscheduler.scheduler import Scheduler
def testjob():
print "This is a test - %s" % datetime.datetime.now()
if __name__ == '__main__':
tp = ProcessPoolExecutor(12)
sched = Scheduler({"apscheduler.threadpool":tp})
sched.start()
sched.add_cron_job(testjob, second="*/5")
raw_input("Press enter to quit\n\n")
sched.shutdown()
However, when I run it I get:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/queues.py", line 266, in _feed
send(obj)
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
If I replace ProcessPoolExecutor with ThreadPoolExecutor everything works fine. I'm guessing that APScheduler takes the function that I pass it in add_cron_job and is making it a member of a class, which the multiprocessing library doesn't handle. Is this something that only works in Python 3? Is there a way to get this to work in Python 2.7? (Some of the libraries that I use haven't been ported to Python 3 yet.)
Thanks,
Brent