1

I have a program P1 (it was written in C# but that is essentially unimportant) that acquires a Win32 mutex. A second program P2 that I need to write in python uses a shared resource with P1. Since this resource is already protected by the Win32 mutex I just need to access the same mutex from python P2. My questions now are:

  1. I have not found any method to access existing windows mutex from the python multiprocessing package. Is there a way to do that?
  2. Alternatively, it could be possible to use the win32event package. But since there is only a very limited documentation with virtually no example code available, I am hesitant to further go in that direction. Do you know of any other resources on pywin32?
  3. It would be nice not to introduce new dependencies. How ever, if there is no other way, do you have suggestions for (well maintained) python packages?

I am just baffled that I could not find other people with similar questions. This should be a fairly common problem, right?

Edit: My question is different from this question. As explained in the accepted answer, the multiprocessing.Lock method is only able to lock python processes. I however, need to aquire an existing Windows mutex.

Isotope
  • 141
  • 1
  • 5
  • 16
  • Does this answer your question? [Proper use of mutexes in Python](https://stackoverflow.com/questions/3310049/proper-use-of-mutexes-in-python) – dejanualex May 11 '21 at 07:38
  • 1
    Thanks for looking into that. Unfortunately, it does not answer my question completely. The accepted answer is not using a Windows mutex - multiprocessing.Lock is only able to synchronise python processes. – Isotope May 11 '21 at 12:24
  • @Isotope [use `ctypes` to import relevant Win32 API functions](https://github.com/benhoyt/namedmutex)? – Mathias R. Jessen May 11 '21 at 12:36
  • 1
    @MathiasR.Jessen Yes, that would be the perfect solution. But it seems that the repository is not very well maintained. Also, the short example provided in the ReadMe does not work anymore. I am receiving an `ArgumentError: argument 3: : wrong type`. – Isotope May 11 '21 at 14:04
  • @MathiasR.Jessen I found a solution to the commented problem above. It is best described [in this question](https://stackoverflow.com/questions/55922044/is-there-a-method-to-implement-mutex-on-python-3-3). – Isotope May 12 '21 at 13:33
  • @Isotope Nice, does that solve your problem then? – Mathias R. Jessen May 12 '21 at 13:42
  • 1
    @MathiasR.Jessen Yes, that solves my problem! If you write an answer I would be happy to accept it. – Isotope May 12 '21 at 13:45

0 Answers0