CoInitialize error when running a python script from APScheduler

3,027 views
Skip to first unread message

Anthony Bouttell

unread,
Jun 16, 2017, 3:29:22 PM6/16/17
to APScheduler
I'm trying to schedule a job to run every 30 minutes, beginning on the qtr. hour:

import time
from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler()

def job():
    t=time.strftime("%Y-%m-%d %H:%M:%S")
    runfile('C:/temp/KIC53_Monitor.py', wdir='C:/temp')

sched.add_job(job, 'cron', minute='15,45', second=0)
sched.start()

When the KIC53_Monitor.py runs I'm getting this error:
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

I'm using anaconda./spyder v3.1.4) python v3.6 apscheduler v3.3.1

Alex Grönholm

unread,
Jun 16, 2017, 5:32:25 PM6/16/17
to apsch...@googlegroups.com

I don't see what this has to do with APScheduler when the external script fails.

--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony Bouttell

unread,
Jun 16, 2017, 7:57:34 PM6/16/17
to APScheduler
My initial scheduler was "schedule":

    import schedule
    import time
    def job():
        t=time.strftime("%Y-%m-%d %H:%M:%S"
        runfile('C:/temp/KIC53_Monitor.py', wdir='C:/temp')
        print("Job run at: ", t)
    schedule.every(30).minutes.do(job)
    while True:
        schedule.run_pending()
        time.sleep(5)

The run-file, (KIC53_Monitor.py), runs perfectly well in this script.
I noticed that this type of scheduling was introducing "creep" into the run time, ( each iteration, was adding about a second to the actual start time). 
I replaced "schedule" with "apschedule" and then began receiving this error.
It is only the run-file, combined with the "apschedule", that caused this error. If I substitute a print command for the run-file, "apschedule" works fine, and if I execute the run-file, using "schedule", or just by itself with no scheduler, it runs well too. 
My conclusion is that "apschedule", executing KIC53_Monitor.py, is the issue.
I'll recheck, and see if I've got some typo somewhere that I didn't notice. 

Anthony Bouttell

unread,
Jun 16, 2017, 10:20:42 PM6/16/17
to APScheduler
got it!
I'm using import win32com.client as com.
as soon as I figure out how to handle this, I'll be golden.

Anthony Bouttell

unread,
Jun 18, 2017, 9:59:02 PM6/18/17
to APScheduler
I thought I understood what was going on, but now.. not so much.

I've reduced the problem to this:

    from apscheduler.schedulers.background import BackgroundScheduler
    sched = BackgroundScheduler()   
    import win32com.client as com
    def job():
        outlook = com.Dispatch("Outlook.Application")
        #do some thing with outlook
        X = 1 
        print(X)
    sched.add_job(job, 'cron',second=0 )  
    sched.start()

This result in this error message:

Job "job (trigger: cron[second='0'], next run at: 2017-06-18 15:46:00 HST)" raised an exception   <<===== the error is being raised on the next run. whether it is scheduled on the next minute or the next day
Traceback (most recent call last):                                                                                                          am i using using apscheduler correctly?
  File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
    retval = job.func(*job.args, **job.kwargs)
  File "C:\temp\SQL\Win32ComTest.py", line 17, in job
    outlook = com.Dispatch("Outlook.Application")
  File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
Job "job (trigger: cron[second='0'], next run at: 2017-06-18 15:47:00 HST)" raised an exception
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)














On Friday, June 16, 2017 at 11:32:25 AM UTC-10, Alex Grönholm wrote:

Alex Grönholm

unread,
Jun 19, 2017, 4:10:12 AM6/19/17
to apsch...@googlegroups.com

Look, your problem is not an APScheduler problem. As the log entry says, there is an exception raised inside the scheduled job. Do you not see that from the traceback?

Anthony Bouttell

unread,
Jun 20, 2017, 4:37:19 PM6/20/17
to APScheduler
Yes, I do see that it's not apscheduler causing the exceptions per se. I just wanted to make certain I was using apscheduler correctly, before I started digging into a bunch of Anaconda libraries, to see what's going on.
It turns out using MS Task Scheduler to run the python script was far more efficient use of my time. 
Thanks for your help though, it was enlightening :) 

Alex Grönholm

unread,
Jun 21, 2017, 3:31:57 AM6/21/17
to apsch...@googlegroups.com

If your example script was all of the code you were using then it's wrong. You were using the background scheduler but then you let the execution of the program get to the end of the script, terminating the application. The BlockingScheduler class exists for cases like this.

Reply all
Reply to author
Forward
0 new messages