我正在创建一个小的python脚本来创建n个线程,每个线程在我的web应用程序上调用curl m次。
将调用该脚本。/multithadedCurl.py 10 100
我希望curl最好执行10*100 = 1000次。但是,我看到它创建了n个线程,但每个线程只调用curl一次。
这是因为我在使用子进程吗?
Python版本Python 2.7.2操作系统: Mac OSX 10.8.2 (Mountain Lion)
任何帮助都非常感谢,我对python非常陌生,这是我开发python的第二天。
`#!/usr/bin/python`import threading
import time
import subprocess
import sys
import math
# Define a function for the thread
def run_command():
count = 0
while (count < int(sys.argv[2])):
subprocess.call(["curl", "http://127.0.0.1:8080"])
count += 1
threadCount = 0
print sys.argv[0]
threadLimit = int(sys.argv[1])
while threadCount < threadLimit:
t=threading.Thread(target=run_command)
t.daemon = True # set thread to daemon ('ok' won't be printed in this case)
t.start()
threadCount += 1`发布于 2012-11-11 18:15:50
通过设置t.daemon = True,您可以说
http://docs.python.org/2/library/threading.html可以将线程标记为“守护进程线程”。这个标志的意义在于,当只剩下守护进程线程时,整个Python程序就会退出。初始值继承自创建线程。可以通过daemon属性设置该标志。
因此,您应该使用t.daemon = False,或者使用join等待所有线程完成。
threads = []
while len(threads) < threadLimit:
t=threading.Thread(target=run_command)
threads.append(t)
t.daemon = True
t.start()
[thread.join() for thread in threads]https://stackoverflow.com/questions/13330028
复制相似问题