forked from Maverick2318/python_parallel_command_execution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_cmd.py
More file actions
executable file
·31 lines (24 loc) · 755 Bytes
/
parallel_cmd.py
File metadata and controls
executable file
·31 lines (24 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
import asyncio
import time
def print_time(word):
print(f"{word} at {time.strftime('%X')}")
async def run_command(cmd):
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
print(f'[{cmd!r} exited with {proc.returncode}]')
if stdout:
print(f'[stdout]\n{stdout.decode()}')
if stderr:
print(f'[stderr]\n{stderr.decode()}')
async def main():
await asyncio.gather(
run_command('sleep 1; echo "hello"'),
run_command('sleep 2; echo "world"'))
asyncio.run(run_command('ls /zzz'))
print_time("Started")
asyncio.run(main())
print_time("Finished")