Python Forum
threads and subprocess.call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
threads and subprocess.call
#4
Using call or run it will not run subprocesses in parallel.
Popen release so it run in parallel.
Here a test.
Running this it wait 20-sec then do ping one bye one.
from subprocess import Popen, call, run

# ping -c 4 google.com ## Linux
commands = [
    'sleep 20',
    'ping -n 4 google.com',
    'ping -n 4 cnn.com'
]
# Try run in parallel
processes = [call(cmd, shell=True) for cmd in commands]

'''
for p in processes:
    p.wait()'''
Now you see that ping and sleep start in parallel all at once.
from subprocess import Popen, call, run

# ping -c 4 google.com ## Linux
commands = [
    'sleep 20',
    'ping -n 4 google.com',
    'ping -n 4 cnn.com'
]
# Try run in parallel
processes = [Popen(cmd, shell=True) for cmd in commands]

'''
for p in processes:
    p.wait()'''
Reply


Messages In This Thread
threads and subprocess.call - by Skaperen - Dec-26-2019, 06:01 AM
RE: threads and subprocess.call - by Gribouillis - Dec-26-2019, 06:39 AM
RE: threads and subprocess.call - by Skaperen - Dec-26-2019, 07:43 AM
RE: threads and subprocess.call - by snippsat - Dec-26-2019, 09:05 AM
RE: threads and subprocess.call - by Skaperen - Dec-27-2019, 12:13 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020