configuration allow multiple instances
pycharm pro 20223.2
I have added my python script to systemd. When working on the script and running it in pycharm I get a second instance of the script which makes my script break. In the configuration settings I have disabled "allow multiple instances" but pycharm does not check if the task is already running on my system and does not show a message.
I also miss the feature mentioned in some discussions to add a "before launch" action using a shell command?
I am running the script in ubuntu 22.04. How can I prevent multiple instances?
请先登录再写评论。
Well, since you have mentioned that your code breaks if another instance of the python process is running, it makes it a coding problem and it would make sense to write the app that would not break under this condition.
For example, you can add a function to your code that would search for the process and kill it before re-running.
Some examples:
https://stackoverflow.com/questions/1705077/python-library-for-linux-process-management
https://stackoverflow.com/questions/160245/which-is-the-best-way-to-get-a-list-of-running-processes-in-unix-with-python
Or, you can simply run a shell script from the python code using the `os` or `subprocess` module.
But if you prefer to run a shell script before launch using the IDE, you can do so by creating a shell run configuration, and adding this configuration to "Before launch" section of your Python script.