0

curln 個のスレッドを作成し、各スレッドが Web アプリで m 回呼び出す小さな python スクリプトを作成しています。

スクリプトが呼び出されます ./multithreadedCurl.py 10 100

curl が 10*100 = 1000 回実行されることを期待しています。ただし、n個のスレッドを作成していることがわかりますが、各スレッドはcurlを1回しか呼び出していません。
これは、サブプロセスを使用しているという事実によるものですか?

Python バージョン Python 2.7.2 OS: Mac OSX 10.8.2 (Mountain Lion)

どんな助けでも大歓迎です。私はPythonに非常に慣れていません。これは、Python開発の2日目です。

#!/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`
4

1 に答える 1

1

設定t.daemon = Trueすることで、あなたはそれを言う

http://docs.python.org/2/library/threading.html スレッドは「デーモン スレッド」としてフラグを立てることができます。このフラグの意味は、デーモン スレッドだけが残ったときに Python プログラム全体が終了することです。初期値は作成スレッドから継承されます。フラグはデーモン プロパティを介して設定できます。

したがって、 を使用する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]
于 2012-11-11T10:15:49.823 に答える