【詳細】Pythonマルチスレッドの実装
9939 ワード
マルチスレッドとは、複数のスレッドを同時に実行することを意味し、一般的には、複数のことを同時に行うことです.
私たちは以前Pythonプログラムをたくさん書いたことがありますが、多くの方法を定義しています.メインメソッドmain()の各関数は順番に行われています.
マルチスレッドがしなければならないのは、これらの関数を同時に実行させることです.
以下、例を挙げて説明する.
単一スレッド:
音楽を闻いてから映画を见て、音楽を闻く时は1秒、2回、映画を见る时は5秒、2回
実行結果は次のとおりです.
マルチスレッドは、映画を見ながら音楽を聴くものです.以下のようにします.
メインメソッドでスレッドを実行します.
【注意】必ずt.join()を付ける
この関数はthreadsスレッドの実行が終了してからメインスレッドに入ることを示しています.
ここでのメインスレッドとは
このスクリプトの実行効果は次のとおりです.
複数のことが同時に行われ、マルチスレッドの意味はここにある!
私たちは以前Pythonプログラムをたくさん書いたことがありますが、多くの方法を定義しています.メインメソッドmain()の各関数は順番に行われています.
マルチスレッドがしなければならないのは、これらの関数を同時に実行させることです.
以下、例を挙げて説明する.
単一スレッド:
#coding=utf-8
import sys
import datetime
from time import ctime,sleep
def music(func):
for i in range(2):
print "I was listening to %s. %s" %(func,ctime())
sleep(1)
def move(func):
for i in range(2):
print "I was at the %s! %s" %(func,ctime())
sleep(5)
if __name__ == '__main__':
print "===%s start===%s"%(sys.argv[0], datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"))
music(u' ')
move(u' ')
print "all over %s" %ctime()
print "===%s start===%s"%(sys.argv[0], datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"))
音楽を闻いてから映画を见て、音楽を闻く时は1秒、2回、映画を见る时は5秒、2回
実行結果は次のとおりです.
===C:/Users/Administrator/PycharmProjects/planBilibili/thread1.py start===2015-12-10 17:05:52
I was listening to . Thu Dec 10 17:05:52 2015
I was listening to . Thu Dec 10 17:05:53 2015
I was at the ! Thu Dec 10 17:05:54 2015
I was at the ! Thu Dec 10 17:05:59 2015
all over Thu Dec 10 17:06:04 2015
===C:/Users/Administrator/PycharmProjects/planBilibili/thread1.py start===2015-12-10 17:06:04
私たちは、確かに順番に行われているのを見て、一度聞いてからもう一度聞いています.一度見てからもう一度見るので、運行時間は12秒です.マルチスレッドは、映画を見ながら音楽を聴くものです.以下のようにします.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import threading
from time import ctime,sleep
import sys
import datetime
def music(func):
for i in range(2):
print "I was listening to %s. %s" %(func,ctime())
sleep(1)
def move(func):
for i in range(2):
print "I was at the %s! %s" %(func,ctime())
sleep(5)
threads = []
t1 = threading.Thread(target=music,args=(u' ',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u' ',))
threads.append(t2)
if __name__ == '__main__':
print "===%s start===%s"%(sys.argv[0], datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"))
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print "all over %s" %ctime()
print "===%s start===%s"%(sys.argv[0], datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"))
マルチスレッドの実装、まずthreadingモジュールをインポートimport threading
スレッド配列を作成し、メソッドをスレッドに追加threads = []
t1 = threading.Thread(target=music,args=(u' ',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u' ',))
threads.append(t2)
メインメソッドでスレッドを実行します.
for t in threads:
t.setDaemon(True)
t.start()
t.join()
【注意】必ずt.join()を付ける
この関数はthreadsスレッドの実行が終了してからメインスレッドに入ることを示しています.
ここでのメインスレッドとは
print "all over %s" %ctime()
print "===%s start===%s"%(sys.argv[0], datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"))
このスクリプトの実行効果は次のとおりです.
===C:/Users/Administrator/PycharmProjects/planBilibili/thread2.py start===2015-12-10 17:13:56
I was listening to . Thu Dec 10 17:13:56 2015
I was at the ! Thu Dec 10 17:13:56 2015
I was listening to . Thu Dec 10 17:13:57 2015
I was at the ! Thu Dec 10 17:14:01 2015
all over Thu Dec 10 17:14:06 2015
===C:/Users/Administrator/PycharmProjects/planBilibili/thread2.py start===2015-12-10 17:14:06
運転時間10秒.音楽を聴くことと映画を見ることができますが、同時に行われています!複数のことが同時に行われ、マルチスレッドの意味はここにある!