I am creating a small python script to create n number of threads and each thread invoking curl m times on my web app.
The script is invoked
./multithreadedCurl.py 10 100
I expect the curl to b executed 10*100 = 1000 times.
However I see that it is creating n threads but each thread is only invoking curl only once.
Is this due to the fact that am using subprocess?
Python version Python 2.7.2
OS: Mac OSX 10.8.2 (Mountain Lion)
Any help much appreciated and I am very new to python and this is my second day of python development.
#!/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`
By setting
t.daemon = Trueyou say thathttp://docs.python.org/2/library/threading.html
A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property.
So you should either use
t.daemon = Falseor wait for all the threads to complete withjoin.