Pythonマルチスレッド(01)


Python-マルチスレッド
プロセス-プロセスには独自の完全に独立した実行環境があり、マルチプロセスがデータを共有するのは問題です.
スレッド-1つのプロセスが独立して実行されるフラグメント.1つのプロセスに複数のスレッドを持つことができます.
グローバルインタプリタ(GTL)
-pythonコードの実行はpython仮想マシンによって制御されます-メインサイクルでは1つの制御スレッドしか実行できません
pythonパッケージ
-thread:以前に適用されたバージョンでpython 3が_に変更されました.thread-threading:先行通行のバッグ
ケース
'''
  time        ,    ,        
'''
import time
def loop1():
    #ctime      
    print("start loop1 at ",time.ctime())
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2():
    print("start loop2 at ", time.ctime())
    # sleep
    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    loop1()
    loop2()
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()

運行結果は以下の通りである:start at Mon Jan 7 09:18:44 2019 start loop 1 at Mon Jan 7 09:18:44 2019 end loop 1 at Mon Jan 7 09:18:48 2019 start loop 2 at Mon Jan 7 09:18:48 2019 end loop 2 at Mon Jan 7 09:18:50 2019 all done at Mon Jan 7 09:18:50 2019
ケース2(ケース1はマルチスレッドを使用)
def loop1():
    #ctime      
    print("start loop1 at ",time.ctime())
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2():
    print("start loop2 at ", time.ctime())
    # sleep
    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    #                    
    threading.Thread(target=loop1).start()
    threading.Thread(target=loop2).start()
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()

運行結果は以下の通りである:start at Mon Jan 7 09:31:46 2019 start loop 1 at Mon Jan 7 09:31:46 2019 start loop 2 at Mon Jan 7 09:31:46 2019 all done at Mon Jan 7 09:31:46 2019 end loop 2 at Mon Jan 7 09:31:48 2019 end loop 1 at Mon Jan 7 09:31:50
ケース3(マルチスレッド+パラメータ)
import time
import threading
import  _thread as thread
def loop1(in1):
    #ctime      
    print("start loop1 at ",time.ctime())
    print("gram1",in1)
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2(in1,in2):
    print("start loop2 at ", time.ctime())
    # sleep
    print("gram1",in1,"gram2",in2)

    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    #                    
    #         start_new_thread
    #               ,           
    #               
    thread.start_new_thread(loop1,("beijing",))
    thread.start_new_thread(loop2,("tianjin","hangzhou"))
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()
        while True:
            time.sleep(8)

運行結果は以下の通りである:start at Mon Jan 7 09:50:48 2019 all done at Mon Jan 7 09:50:48 2019 start loop 1 at Mon Jan 7 09:50:48 2019 gram 1 beijing start loop 2 at Mon Jan 7 09:50:48 2019 gram 1 tianjin gram 2 hangzhou end loop 2 at Mon Jan 7 09:50:50 2019 end loop 1 at Mon Jan 7 09:50:52 2019
threadingの使用
-t=threading.Thread(target=XXX,args=(xxx,)-t.start():マルチスレッドの起動-t.join():マルチスレッドの実行完了を待つ
ケース04
import time
import threading
import  _thread as thread
def loop1(in1):
    #ctime      
    print("start loop1 at ",time.ctime())
    print("gram1",in1)
    #sleep
    time.sleep(4)
    print("end loop1 at",time.ctime())
def loop2(in1,in2):
    print("start loop2 at ", time.ctime())
    # sleep
    print("gram1",in1,"gram2",in2)

    time.sleep(2)
    print("end loop2 at", time.ctime())
def main():
    print("start at",time.ctime())
    #                    

    t1=threading.Thread(target=loop1,args=("beijing",))
    t2=threading.Thread(target=loop2, args=("tianjin","hangzhou"))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print("all done at ",time.ctime())

if __name__=="__main__":
        main()

運行結果は以下の通りである:start at Mon Jan 7 10:06:10 2019 start loop 1 at Mon Jan 7 10:06:10 2019 gram 1 beijing start loop 2 at Mon Jan 7 10:06:10 2019 gram 1 tianjin gram 2 hangzhou end loop 2 at Mon Jan 7 10:06:12 2019 end loop 1 at Mon Jan 7 10:06:14 2019 all done at Mon Jan 7 10:06:14 2019
デーモンスレッド
-プログラムでサブスレッドがデーモンスレッドに設定されている場合、サブスレッド恵子ああメインプログラムが終了すると自動的に終了します-デーモンスレッドは重要ではないか、メインスレッドから独立して実行することは許されないと考えられています5
import time
import threading
import  _thread as thread
def fun():
    print("start fun")
    time.sleep(2)
    print("end fun")
print("main thraed")

t1=threading.Thread(target=fun,args=())
# t1       
t1.setDaemon(True)
t1.start()
time.sleep(1)
print("main thread dead")

実行結果は次のとおりです:main thraed start fun main thread dead
スレッドでよく使用されるプロパティ
-threading.CurrentThread:現在のスレッドを返す変数-threading.Enumerate:実行中のスレッドを含むlist-threadingを返します.ActiveCount:実行中のスレッドの数を返す-thr.setName:スレッドの名前を設定-thr.getName:スレッドの名前を取得
直接threadから継承する.Thread
-Threadを直接継承-run関数を書き換える-クラスインスタンスはケース6を直接実行できます
import time
import threading
#      threading.Thread
class MyThread(threading.Thread):
    def __init__(self,arg):
        super(MyThread, self).__init__()
        self.arg=arg
    #    run  ,        
    def  run(self):
        time.sleep(2)
        print("the args for this class is {0}".format(self.arg))
for  i  in range(5):
    t=MyThread(i)
    t.start()
    t.join()
print("main thread is done")

実行結果は、the args for this class is 0 the args for this class is 1 the args for this class is 2 the args for this class is 3 the args for this class is 4 main thread is done