Shell实现多进程:
1..使用&和wait配合实现shell多进程并行
参考连接:https://blog.csdn.net/yuefei169/article/details/83340480
2..改串行执行为并行执行方式:将前台执行命令放在后台执行(串行命令后加 &符号).(生产少使用该方式)
3..使用元祖模拟队列来控制进程数量:for循环子进程PID作为队列元素,模拟限定最大进程数队列($!获取PID)
4.使用fifo管道特性来控制进程数量:
python多线程执行shell
import concurrent.futures
def worker():
print('Worker thread started')
# do some work here
print('Worker thread finished')
if __name__ == '__main__':
print('Main thread started')
# create a thread pool
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
# submit worker function to the pool
future = executor.submit(worker)
print('Main thread finished')
# 创建多进程下载download.py
cat << EOF > $WORKSPACE/download.py
import multiprocessing
import subprocess
def execute_shell_command(command):
subprocess.call(command, shell=True)
processes = []
commands = [
"git clone --single-branch ssh://XXX -b ${XXX}",
"git clone --single-branch ssh://YYY -b ${YYY}",
]
for command in commands:
process = multiprocessing.Process(target=execute_shell_command, args=(command,))
processes.append(process)
process.start()
EOF
# 执行download.py脚本
cat $WORKSPACE/download.py
mkdir src && cd $WORKSPACE/src
python3 $WORKSPACE/download.py
echo "All processes have finished"