docx2pdf
docx2pdf copied to clipboard
convert function sometimes fails in a multithreaded context
Configuration:
- docx2pdf: 0.1.7
- python: 3.7.7
- os: windows 10 version 2004 (build 19041.508)
The convert
function will sometimes yield (-2147221008, 'CoInitialize has not been called.', None, None)
. I'm currently working to figure out the exact steps to reproduce, but I might not be able to, since the overall application is multithreaded (via concurrent.futures.ThreadPoolExecutor
, where one of the tasks calls convert
) and seems to happen at random.
As far as I know, using win32com
in Python requires calling pythoncom.CoInitialize()
- see this post for an example.
it seems like the SO post is referring to keeping a single COM application alive to re-use in multiple threads but still in a somewhat .. sequential manner if I understand correctly? they want to re-use it in subsequent ajax requests
I think what you're trying to do seems a little bit different in that you want to use it concurrently
perhaps you can try using concurrent.futures.ProcessPoolExecutor
to start to see if concurrency with win32com works at all? if so, we can re-visit getting it to work with threads
I think what you're trying to do seems a little bit different in that you want to use it concurrently
Yes, precisely, I'm trying to use concurrently.
[...] to see if concurrency with win32com works at all
Yes, I plan to build a minimum reproducible example as soon as possible, I just haven't had the time.
On the other hand, I did get around this issue, at least as a short-term solution, as below, for all calls in all threads:
pythoncom.CoInitialize()
docx2pdf.convert(...)
related to #28
Same issue here. What should we do ?
EDIT:
My probolem solved with @flriancu 's solution. Here is a thread which describes what exactly this method does :
https://tousu.in/qa/?qa=676900/python-win32com-client-dispatch-cherrypy-coinitialize-has-not-been-called
I had the exact same problem. This solution also worked for me. Thanks @flriancu
pythoncom.CoInitialize() docx2pdf.convert(...)