0

I use a Python program to manage a MySQL database and I want to run the program and database on a virtual machine so that multiple users can access and work with it. I have two questions about this:

  1. If multiple users were using the Python script, would they affect each other's global variables?
  2. Is it possible with the operating system or with Python to only allow one user at the same time to use the application?
1

If multiple users were using the Python script, would they affect each other's global variables?

No. The script itself is just an ordinary text file; it has no state attached to it. All global variables belong not to the script but to the process that is executing it.

It doesn't even matter if it's run by the same user or by different users. Every time you run the script, the OS starts a new process with a fresh copy of the interpreter, which itself starts as a "blank slate" – it doesn't load any state and doesn't store any state unless you code that into the script itself.

(Open a Notepad window. Open a second Notepad window. Type some text into both. Notice that each window can have different text in it, even though both are the same Notepad.exe. That's actually not a good analogy, but it might do the job anyway.)

Is it possible with the operating system or with Python to only allow one user at the same time to use the application?

You will need to write Python code that enforces such limits, and the OS might provide features to help with that. Search for "locking".

(However, many methods only work within the same user account, or at least require root/administrator privileges to set up – for example, a 'mutex' or a 'semaphore' on Windows can only be made global by administrators, otherwise it's only visible to the same user who created it.)

One way is to rely on file locking. Have the script open an empty file and use fcntl.flock() on Linux/macOS, or win32file.LockFileEx() on Windows.

Because the script manages a MySQL database, you could also keep state within the database itself. It should be reliable to use GET_LOCK() and RELEASE_LOCK() SQL functions. (Some tools also do this by creating a separate "locking" table with entries to indicate what's locked, but that's tricky to get right.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.