Running multiple python scripts at the same time with a different CMD name

I am trying to call a.py and b.py at the same time in test.py using multiprocessing. Process (), it worked. But the CMD process name for the a.py, b.py, and test.py files, which are "/usr/bin/python/tmp/test.py", are the same.

# ps -ef | grep b.py UID PID PPID C STIME TTY TIME CMD 501 61486 39878 0 2:33PM ?? 0:00.05 /usr/bin/python /tmp/test.py 501 61487 61486 0 2:33PM ?? 0:00.01 /usr/bin/python /tmp/test.py 501 61488 61486 0 2:33PM ?? 0:00.01 /usr/bin/python /tmp/test.py 

I would like these three processes to map different CMD names to "ps -ef" as shown below: (which may help me determine if another process is working.)

 # ps -ef | grep b.py UID PID PPID C STIME TTY TIME CMD 501 61486 39878 0 2:33PM ?? 0:00.05 /usr/bin/python /tmp/test.py 501 61487 61486 0 2:33PM ?? 0:00.01 /usr/bin/python /tmp/a.py 501 61488 61486 0 2:33PM ?? 0:00.01 /usr/bin/python /tmp/b.py 

Please help with advice :)

The source code is as follows:

test.py:

 import multiprocessing import a import b p1 = multiprocessing.Process(target=a.printa) p2 = multiprocessing.Process(target=b.printb) p1.start() p2.start() 

a.py:

 import time def printa(): while True: print 'a' time.sleep(1) if __name__ == '__main__': printa() 

b.py:

 import time def printb(): while True: print 'b' time.sleep(1) if __name__ == '__main__': printb() 
+2
source share
2 answers

You use a.py and b.py as a library in python, so it is called under the same name as the test.py . Either the use of multiprocessing , or joblib , the same situation occurs.

There is a name option in the Process method ( multiprocessing.Process(self, group=None, target=None, name=None, args=(), kwargs={}) ), but, as @fedterzi says, it is intended only for identification purposes .

If you want to call a process or file, you can use the subprocess library.

Depending on the task, such as parellelizing bunch of processes, you can also use the gnu parallel or some other method through bash , to perform the desired behavior.

0
source

Reading Python "2.7.13 Documentation using-the-subprocess-module
Choose a subprocess a NOWAIT method, edit your question code accordingly.

 import subprocess def openCmd(name): return subprocess.? if __name__ == '__main__': while True: key = raw_input('input 1=open, 0=terminate, q=quit:') print(key) if key == '1': A_p = openCmd(('a')) B_p = openCmd(('b')) if key == '0': A_p.terminate() B_p.terminate() if key == 'q': break 

Tested with Python: 2.7.9

0
source

Source: https://habr.com/ru/post/1496879/


All Articles