我需要在同一时间使用python在Linux中执行多个命令。 我不需要按命令运行它。
我尝试编写这段代码,但我无法理解如何使用python同时执行多个命令,我也读过有关python多线程但我不知道如何使用它。
代码:
# -*- coding: utf-8 -*-
import os
commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com']
count = 0
for com in commands:
print "Start execute commands.."
os.system(com)
count += 1
print "[OK] command "+str(count)+" runing successfully."
else:
print "Finish.."
请问我如何使用python执行此操作并同时执行多个命令?
答案 0 :(得分:2)
看起来像是典型的生产者 - 消费者问题
import threading
import os
commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com']
def worker_func():
while commands: # Checks if the the list is not-empty. Loop exits when list is becomes empty
com = commands.pop(0)
print "Start execute commands.."
os.system(com)
count += 1
print "[OK] command "+str(count)+" runing successfully."
workers = [threading.Thread(target=worker_func, args=tuple(), name='thread_'+str(i)) for i in range(5) ] # Create 5 workers (consumers)
[worker.start() for worker in workers] # Start working
[worker.join() for worker in workers] # Wait for all workers to finish
这里我创建了5个工作线程。这些线程将运行函数worker_func
worker_func
将从列表中选取一个元素并执行该作业。当列表变空时,函数返回(退出)。
注意:阅读Global Interpreter Lock以了解不应使用python多线程的位置。
在这种情况下,GIL(全局解释器锁)不应该影响您,因为worker_func
调用子进程并等待它完成。当线程正在等待GIL被释放到其他线程。
答案 1 :(得分:2)
我建议两种解决方案,但有很多
简单解决方案:
在命令末尾使用feed_dict
在后台运行它们:
&
线程+队列解决方案,控制最大线程产生:
commands = ['ping www.google.com &', 'ping www.yahoo.com &', 'ping www.hotmail.com &']
for com in commands:
os.system(com) # now commands will run in background
答案 2 :(得分:1)
我的解决方案没有启动额外的线程
我使用subprocess.Popen
运行命令,在第一个循环的列表中存储Popen
个对象,并等到子进程在第二个循环中完成
from subprocess import Popen, PIPE
commands = ['ping www.google.com', 'ping www.yahoo.com', 'dir']
count = 0
processes = []
for com in commands:
print "Start execute commands.."
processes.append(Popen(com, shell=True))
count += 1
print "[OK] command "+str(count)+" running successfully."
else:
print "Finish.."
for i, process in enumerate(processes):
process.wait()
print "Command #{} finished".format(i)
答案 3 :(得分:0)
import threading
import os
def ping_url(number):
os.system(number)
thread_list = []
commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com']
for url in commands:
# Instantiates the thread
t = threading.Thread(target=print_number, args=(url,))
# Sticks the thread in a list so that it remains accessible
thread_list.append(t)
# Starts threads
for thread in thread_list:
thread.start()
# This blocks the calling thread until the thread whose join() method is called is terminated.
# From http://docs.python.org/2/library/threading.html#thread-objects
for thread in thread_list:
thread.join()
# Demonstrates that the main process waited for threads to complete
print "Done"