Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run same python code in two terminals, will them interfere each other?

Tags:

python

ubuntu

I have a python script which takes a while to finish its executing depending on the passed argument. So if I run them from two terminals with different arguments, do they get their own version of the code? I can't see two .pyc files being generated.

Terminal 1 runs: python prog.py 1000 > out_1000.out

Before the script running on terminal 1 terminate, i start running an another; thus terminal 2 runs: python prog.py 100 > out_100.out

Or basically my question is could they interfere with each other?

like image 994
samsamara Avatar asked Apr 29 '16 07:04

samsamara


People also ask

Can you run the same python script at the same time?

You can run multiple instances of a python script from a shell however from within a python program without the use of multithreading/multiprocessing the GIL limitation will impact what you are trying to do.

Can I run two python scripts at the same time in Vscode?

If you need to coordinate execution and communicate between these programs, you'll need to use threading. If the scripts can run independently, you can run them manually at the same time from a terminal, or use a subprocess call from the first script: subprocess.

How run two files simultaneously in VS code?

Once the multiple file support option is enabled, you can use your mouse to select how to open a new file in the program: Launch VS Code and press the “Ctrl” and “P” keys at the same time to search for a file to open in the current project. Type in the file name.

Can a python script call another python script?

Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.


1 Answers

If you are writing the output to the same file in disk, then yes, it will be overwritten. However, it seems that you're actually printing to the stdout and then redirect it to a file. So that is not the case here.

Now answer to your question is simple: there is no interaction between two different executions of the same code. When you execute a program or a script OS will load the code to the memory and execute it and subsequent changes to code has nothing to do with the code that is already running. Technically a program that is running is called a process. Also when you run a code on two different terminals there will be two different processes on the OS one for each of them and there is no way for two process to interfere unless you explicitly do that (IPC or inter-process communication) which you are doing here.

So in summary you can run your code simultaneously on different terminals they will be completely independent.

like image 87
Ali Avatar answered Sep 19 '22 08:09

Ali