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?

0
4 comments
Hi, it sounds like the best solution would be to implement a check for a running process in your code. The IDE itself doesn't check if the process is already running that has been started externally.
0
Thanks for the reply.
Unfortunately it does not help me much.
As I can check for a running task in the main script, when I start the script then a check will find it and prevent it from continuing?
So I would need something that checks before the task starts?
As I have no more the option to run a shell script in the current version of pycharm in the configuration/before launch section, how would I accomplish this?
0

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.

0

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.

0

Please sign in to leave a comment.