5

Edit: too much information for distracting people so I've removed almost everything. This is on windows, python 3.6 using mmap and msvcrt

Code in one python application:

fd = os.open(r'C:\somefile', os.O_CREAT | os.O_RDWR)
mm_file = mmap.mmap(fd, 4096, access=mmap.ACCESS_WRITE)
msvcrt.locking(fd, msvcrt.LK_LOCK, 4096)

Expectation when a second application attempts to open C:\somefile for read/write is to get some kind of an error message that it couldn't access it because it's locked.

What actually happend: the second application accessed it no problem.

Kevin S
  • 930
  • 10
  • 19
  • 1
    Interprocesscommunication: https://docs.python.org/3/library/ipc.html - read up on locks, use named pipes or socket for communication. – Patrick Artner Apr 28 '18 at 15:20
  • btw. asking for libraries is OT. maybe read up on [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) if your questions get downvoted and/or closed fast, it might be the community signaling youre kindof off topic :) - happy coding. Make sure to search for interprocess communication or named pipes on SO there are some nice examples around - not sure if one tailors to your specific locking needs. – Patrick Artner Apr 28 '18 at 15:26
  • Couldn't you should allocate the double amount of memory and one half is only being written to by the one program and the other one just reads and never writes? – MegaIng Apr 28 '18 at 15:30
  • Does one process start the other? You can use python's `multiprocessing` with `mmap`, one side for commands and synchronization, the other for fast access to shared memory. – tdelaney Apr 28 '18 at 15:38
  • @PatrickArtner I've read that document. That gives a nice overview of locks, but back to my question asked, how do I pass a lock between two entirely separate python interpreters? If I wanted the latency involved in socket communication I would use sockets over shared memory, pipes are a solution as long as they're as fast as shared memory and work on windows. – Kevin S Apr 28 '18 at 16:06
  • @MegaIng That is a good idea, and it does solve the potential for data corruption. However, what if program B reads from program A's memory section before A updates, now B has stale data? – Kevin S Apr 28 '18 at 16:07
  • @tdelaney nope, the two processes are independent and one does not start the other – Kevin S Apr 28 '18 at 16:07
  • You could create a file and use locks on that file to control access to the mmap. – tdelaney Apr 28 '18 at 16:46
  • Named pipes work on windows, you can communicate between the proceses over the pipe and tell each other when things are blocker/ - problem is, you would have to synch in code. if you create a file and aquire a file lock on it OS-Software supports you, check https://stackoverflow.com/questions/6931342/system-wide-mutex-in-python-on-linux - same works for windows. one of the answers tells you It might fail. – Patrick Artner Apr 28 '18 at 17:28
  • @tdelaney create a file and use locks on that file, how? – Kevin S Apr 28 '18 at 19:25
  • @PatrickArtner unfortunately the solutions provided in that link are for linux and I'm running on windows. Also, named pipes works for 2 apps but as soon as it scales to 4 or 5 I've got a problem. That is ultimately the cusp of my question though...how do I put a lock on a file in windows with python – Kevin S Apr 28 '18 at 19:26
  • In the past in C/C++ programs I have placed pthread mutexes in the shared memory. However, I do not know of a way to do that in Python. – Christopher Barber Sep 06 '19 at 13:35

0 Answers0