AlJohri / docx2pdf

MIT License
506 stars 96 forks source link

convert function sometimes fails in a multithreaded context #23

Open flriancu opened 3 years ago

flriancu commented 3 years ago

Configuration:

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.

AlJohri commented 3 years ago

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

flriancu commented 3 years ago

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(...)
AlJohri commented 2 years ago

related to #28

WatchDogsDev commented 2 years ago

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

hanusatv commented 2 years ago

I had the exact same problem. This solution also worked for me. Thanks @flriancu

pythoncom.CoInitialize()
docx2pdf.convert(...)