docx2pdf icon indicating copy to clipboard operation
docx2pdf copied to clipboard

convert function sometimes fails in a multithreaded context

Open flriancu opened this issue 3 years ago • 5 comments

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.

flriancu avatar Sep 17 '20 06:09 flriancu

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

AlJohri avatar Sep 17 '20 14:09 AlJohri

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(...)

flriancu avatar Sep 18 '20 08:09 flriancu

related to #28

AlJohri avatar Dec 11 '21 17:12 AlJohri

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

WatchDogsDev avatar Jun 23 '22 12:06 WatchDogsDev

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

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

hanusatv avatar Jul 02 '22 01:07 hanusatv