This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: single instance of program
What is the best way to insure that only 1 copy of a python script is running? I am having trouble with python zombies. I tired creating a write lock using open("lock","w"), but python doesn't notify me if the file already has a write lock, it just seems to wait.
Try:
import os
os.open("lock", os.O_CREAT|os.O_EXCL)
The documentation for os.open and its flags.
Your question is similar to this one: What is the best way to open a file for exclusive access in Python?. The answers given there should help you with your issue.
(Use the flag combination portalocker.LOCK_EX!|portalocker.LOCK_NB to return quickly. If the file is locked by another process, your script should get an exception.)
Related
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
How do I execute a string containing Python, without creating a new file, using excec() or using "os" module?
I've seen the similiar question, but the answeres always kind of created some "new file"(for example created something like this), or went out of the current code, but i want it to stay inside the code and execute it.
Example:
string_base64 = "eCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCk="
This b64 code has inside it strings and commands (+ For example imports) and it needs to run in the current file, without using exec() or modules
This Thread and the answers, didn't answer the question!
Buildin exec function seems to be doing exactly what you want:
https://docs.python.org/3/library/functions.html#exec
This question already has answers here:
Locking a file in Python
(15 answers)
Closed 3 years ago.
This is not a duplicate of Locking a file in Python
I have two scripts, one runs every 30 minutes, the other one runs every one minute.
and they both use the same file to do few things.
at some point, every 30 minute they try to access the same file at the same time and they corrupt the file.
I was thinking about using wait. but they are two independent scripts and I am not sure if this is possible.
Any idea?
I thought about using
with FileLock("document.txt")
The problem that arise is; if script-1 acquire the lock for "document.txt" then script-2 wants to access document.txt, is it going to wait that script-1 finish? or is it going to skip that line of code? as the 2nd one isn't an option?
also. once the lock is acquired, how to remove it when it's no longer needed?
One of the simpliest ways to get this done (in case you have write access to the file's directory) is to create an additional file (like filename.lck) to point out that some script is working on that file. Obviously, once a script has finished working with the file, that lock-file needs to be removed.
But honestly, I would be very surprised if such a locking mechanism is not foreseen in Python. How exactly do you open and close the mentioned file? Maybe some parameter already takes care of the locking.
This question already has answers here:
Time-Limited Input? [duplicate]
(2 answers)
Closed 8 years ago.
I'm starting out with python and want to know how to set a time limit for the user to input something using the input() function. I have looked at other questions and cant find one that:
a) I can understand
b) Works for me.
I am on Python 3.4 and I am a complete noob. Thank you for taking the time to answer my question!:)
I am also on windows.
Here's an earlier post that answers your question.
Basically you use the signal module to send an alarm signal to your program an interrupt the input command after a timeout.
I'm not sure there's a simpler way to do this, but to outline it broadly the "interrupted" function can be any function you like, it gets called when the timeout runs out. When the alarm triggers it raises an exception which sends you to the 'except block' of the input function.
Actually, looking at the example, I think input is a built-in function, so that function should really be called something else!
This question already has answers here:
How can I find the current OS in Python? [duplicate]
(5 answers)
Closed 9 years ago.
I have a program I'm thinking through right now and doing tons of investigating on. One of the stipulations with the program is that I want it so it can only run on Linux. I DO NOT want the program to be usable on Windows or Apple...Linux only. I have my reasons.
I know you can use certain modules(tkinter...root.mainloop()), ie, that will cause a program to not run in Windows if you leave certain things out. Is there a way you can accomplish the same task without using any particular module...just 'base code'?
Just test for Linux:
import platform
import sys
if platform.system() != 'Linux':
sys.stderr('Linux required\n')
sys.exit(1)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Monitoring files/directories with python
I am making an API server that allows hot-deploying of code. And yes, there are dozens of questions about re-importing modules. I just want to monitor a directory, and when a change is detected, perform an action.
path_to_monitor="c:\\app\\package"
def action():
do_something()
dm=dir_monitor(path_to_monitor,action)
dm.start()
I need a solution that is mature and cross-platform.
You can use a Python thread which uses os.listdir() to list files. If you want to monitor other file properties than the file name, os.stat might help you.