【发布时间】:2018-04-10 02:38:12
【问题描述】:
我有一个字典,其中包含 2000 个键值对键作为应用程序的 URL,值作为执行应用程序测试用例的命令。
我需要使用线程并行执行这些,并使用子进程模块启动测试用例命令
在运行一些线程并在标准输出中获得输出后,后续线程卡住并失败。
我尝试检查在我的代码中切出 500 个队列项。它没有受到打击,并且还收到了带有测试用例输出报告的邮件。
请告诉我如何避免在中间敲打螺纹。
我的代码是
import Queue
import subprocess
from threading import Thread
SERVER_COMMANDS = {}
TOTAL_OUTPUT = []
def run_function(server_commands, receivers, url, test_case_name, mail_send=True):
threads = []
q_items = []
counter = 0
for server, command in server_commands.iteritems():
q_items.append(dict(counter=counter,server=server, command=command, server_commands=server_commands))
counter += 1
Q = Queue.Queue()
for item in q_items:
Q.put(item)
for i in xrange(90):
th = Thread(target=_run_function, args=(Q,))
th.start()
threads.append(th)
for th in threads:
th.join()
total_output = ''.join(TOTAL_OUTPUT)
def _run_function(q):
while not q.empty():
args = q.get()
server = args.get('server')
command = args.get('command')
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
process.wait()
output, error = process.communicate()
print "The thread number is %s" % args.get('counter')
SERVER_COMMANDS[server] = output
TOTAL_OUTPUT.append(output)
for line in output:
sys.stdout.write(line)
为了获得更好的视野,请查看Result Screenshot
【问题讨论】:
标签: python multithreading unit-testing subprocess python-multithreading