5

スレッド ライブラリを使用する場合、 start_new_threads によって作成されたすべてのスレッドを結合する方法はありますか?

例えば:

try:    
    import thread 
except ImportError:
    import _thread as thread #Py3K changed it.

for url in url_ip_hash.keys(): 
    thread.start_new_thread(check_url, (url,))

すべてのスレッドに参加するにはどうすればよいですか?

4

2 に答える 2

24

推奨されるThreadingモジュールthreadの代わりに使用している理由はありますか? そうでない場合は、join メソッドを持つオブジェクトを使用する必要があります。threading.Thread

from threading import Thread


def check_url(url):
    # some code

threads = []
for url in url_ip_hash.keys():
    t = Thread(target=check_url, args=(url, ))
    t.start()
    threads.append(t)

# join all threads
for t in threads:
    t.join()
于 2013-10-24T06:30:34.987 に答える