知識點:之前第一節介紹建立執行緒的方法是:通過threading.thread(target=函式名不要加括號)建立乙個物件,通過物件呼叫start方法建立並啟動執行緒;
這一節介紹另外一種建立執行緒的方法:寫乙個子類,繼承thread類,裡面定義乙個run方法即可通過該子類建立乙個執行緒
**如下,解釋看註解:
#!/usr/bin/env python
#coding=utf-8
#author:劉仲
#datetime:2018/7/23 9:57
#software: pycharm
import
threading
import
time
#通過繼承thred類建立執行緒
#執行緒1
class
mythread(threading.thread):
def run(self): #
類裡面必須定義乙個run方法,當呼叫start方法時,會自動執行run方法,run裡面的**就是要執行的子執行緒
self.test1()
deftest1(self):
for i in range(3):
print('
我是執行緒1')
time.sleep(1)
#執行緒2
class
mythread1(threading.thread):
defrun(self):
self.test2()
deftest2(self):
for i in range(3):
print('
我是執行緒2')
time.sleep(1)
if__name__ == '
__main__':
t =mythread()
t1 =mythread1()
t.start()
t1.start()
通過繼承Thread實現多執行緒
package com.freeflying.thread.base classname mythread description 通過繼承thread實現多執行緒 author freeflying date 2018年6月21日 class mythread extends thread cla...
執行緒3 檢視正在執行的執行緒,通過繼承建立執行緒
檢視執行緒數量 通過繼承建立執行緒 import time import threading 用到threading模組中的enumerate方法 來進行檢視正在執行的執行緒 deftext1 函式目的用來停頓5s for i in range 5 print text1 time.sleep 1 ...
Linux網路程式設計(3) 多程序 多執行緒
在我的 多程序 這裡多程序採用傳統的多程序模型。每當有client發來的連線時建立乙個程序來處理連線,乙個子程序相應乙個連線。有了上篇單一程序的基礎,此處僅僅做簡單的改動便能夠實現。while 1 close clientfd 僅僅須要在while裡面加入程序的建立就可以,然後在子程序裡先關閉父程序...