日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python如何开启多线程_Python如何创建多线程

發(fā)布時間:2023/12/4 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何开启多线程_Python如何创建多线程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python提供了_thread和threading兩個模塊來支持多線程,但_thread提供低級別的、原始的縣城支持,以及一個簡單的鎖,通常情況下我們使用后者,來進行多線程編程

創(chuàng)建多線程

使用threading模塊創(chuàng)建線程有兩種方式,一種是調(diào)用Thread類的構(gòu)造器來創(chuàng)建,另一種是繼承Thread類創(chuàng)建線程類

使用Thread類的構(gòu)造器創(chuàng)建線程

Thread類有如下構(gòu)造器,直接調(diào)用它即可創(chuàng)建線程

__init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)

group:指定該線程所屬的線程組

target:指定該線程要調(diào)度的目標方法

args:指定一個元組,以位置參數(shù)的形式為target指定的函數(shù)傳入?yún)?shù)

kwargs:指定一個字典,以關(guān)鍵字參數(shù)的形式為target指定的函數(shù)傳入?yún)?shù)

daemon:指定所構(gòu)建的線程是否是后臺線程

使用構(gòu)造器創(chuàng)建線程步驟

調(diào)用Thread類的構(gòu)造器創(chuàng)建線程對象,其中target參數(shù)指定的函數(shù)作為線程執(zhí)行體

調(diào)用線程對象的start()方法啟動該線程

import threading

# 定義一個普通的action函數(shù),該函數(shù)準備作為線程執(zhí)行體

def action(max):for i inrange(max):

# 調(diào)用threading模塊current_thread()函數(shù)獲取當前線程

# 線程對象的getName()方法獲取當前線程的名字

print(threading.current_thread().getName()+ " " +str(i))

# 下面是主程序(也就是主線程的執(zhí)行體)for i in range(100):

# 調(diào)用threading模塊current_thread()函數(shù)獲取當前線程

print(threading.current_thread().getName()+ " " +str(i))if i == 20:

# 創(chuàng)建并啟動第一個線程

t1=threading.Thread(target=action,args=(100,))

t1.start()

# 創(chuàng)建并啟動第二個線程

t2=threading.Thread(target=action,args=(100,))

t2.start()

print('主線程執(zhí)行完成!')

args=(100,)可以看到這個參數(shù),他是個元組形式的參數(shù),按位置逐一傳給函數(shù)執(zhí)行體,在這個例子中就是action(MAX)函數(shù)

繼承Thread類創(chuàng)建線程類

通過繼承Thread類來創(chuàng)建線程步驟

定義Thread類的子類,并重寫該類的run()函數(shù),run()函數(shù)是線程執(zhí)行體

創(chuàng)建Thread子類的實例,也就是創(chuàng)建線程對象

調(diào)用線程對象的start()方法來啟動線程

————————————————

import threading

# 通過繼承threading.Thread類來創(chuàng)建線程類classFkThread(threading.Thread):

def __init__(self):

threading.Thread.__init__(self)

self.i= 0# 重寫run()方法作為線程執(zhí)行體

def run(self):while self.i < 100:

# 調(diào)用threading模塊current_thread()函數(shù)獲取當前線程

# 線程對象的getName()方法獲取當前線程的名字

print(threading.current_thread().getName()+ " " +str(self.i))

self.i+= 1# 下面是主程序(也就是主線程的執(zhí)行體)for i in range(100):

# 調(diào)用threading模塊current_thread()函數(shù)獲取當前線程

print(threading.current_thread().getName()+ " " +str(i))if i == 20:

# 創(chuàng)建并啟動第一個線程

ft1=FkThread()

ft1.start()

# 創(chuàng)建并啟動第二個線程

ft2=FkThread()

ft2.start()

print('主線程執(zhí)行完成!')

多線程調(diào)用接口

# -*- coding: utf-8 -*-import threading

import requestsfromtime import ctime

import json

def syc_owen():"""syc_owen"""print("Start synchronizing %s %s" % ("owen", ctime()))

parameter= {"userId":"1263","userName":"13810078954","enterpriseId":"10330","flag":"sended"}

request_own= requests.put("https://xxxxxx.leadscloud.com/mail/receiveSendedAndRubbishMail", data=parameter)

data=request_own.json()

print(json.dumps(data, indent=4, sort_keys=True, ensure_ascii=False) )

print("接口調(diào)用已經(jīng)返回結(jié)果,本次同步結(jié)束")

def syc_grace():"""syc_grace"""print("Start synchronizing %s %s" % ("grace", ctime()))

parameter= {"userId":"1294","userName":"13810327625","enterpriseId":"10330","flag":"sended"}

request_own= requests.put("https://xxxxxx.leadscloud.com/mail/receiveSendedAndRubbishMail", data=parameter)

data=request_own.json()

print(json.dumps(data, indent=4, sort_keys=True, ensure_ascii=False) )

print("接口調(diào)用已經(jīng)返回結(jié)果,本次同步結(jié)束")

syc_email_threads=[] # 定義線程列表,然后逐一將線程放進列表最后加載線程列表執(zhí)行

owen_thread= threading.Thread(target=syc_owen)

syc_email_threads.append(owen_thread)

grace_thread= threading.Thread(target=syc_grace)

syc_email_threads.append(grace_thread)if __name__ == '__main__':for threademail insyc_email_threads:

threademail.start() # 啟動線程活動for threademail insyc_email_threads:

threademail.join() # 等待線程終止

多線程啟動瀏覽器實例

單線程代碼

# -*- coding: utf-8 -*-

"""Created on Wed Apr 24 11:45:55 2019@author: davieyang"""fromselenium import webdriver # 引入webdriverfromtime import sleep # 從time模塊引入sleepfromtime import ctime # 從time模塊引入ctime

def start_chrome(): # 定義啟動谷歌瀏覽器的方法

print("starting chrome browser now! %s" %ctime()) # 控制臺打印當前時間

chrome_driver=webdriver.Chrome()

chrome_driver.get("http://www.baidu.com") #打開百度主頁

sleep(2)

chrome_driver.quit()

def start_firefox(): # 定義啟動火狐瀏覽器的方法

print("starting firefox browser now! %s" %ctime()) # 控制臺打印當前時間

fire_driver=webdriver.Firefox()

fire_driver.get("http://www.baidu.com") #打開百度主頁

sleep(3)

fire_driver.quit()

def start_ie(): # 定義啟動IE瀏覽器的方法

print("starting ie browser now! %s" %ctime()) # 控制臺打印當前時間

ie_driver=webdriver.Ie()

ie_driver.get("http://www.baidu.com") #打開百度主頁

sleep(5)

ie_driver.quit()if __name__ == '__main__':

start_chrome()

start_firefox()

start_ie()

print(u"全部結(jié)束 %s" %ctime())

多線程代碼

# -*- coding: utf-8 -*-

"""Created on Wed Apr 24 19:23:25 2019@author: davieyang"""

fromselenium import webdriverfromtime import sleepfromtime import ctime

import threading

def start_chrome(): # 定義啟動谷歌瀏覽器的方法

print("starting chrome browser now! %s" %ctime())

chrome_driver=webdriver.Chrome()

chrome_driver.get("http://www.baidu.com")

sleep(2)

chrome_driver.quit()

def start_firefox(): # 定義啟動火狐瀏覽器的方法

print("starting firefox browser now! %s" %ctime())

fire_driver=webdriver.Firefox()

fire_driver.get("http://www.baidu.com")

sleep(3)

fire_driver.quit()

def start_ie(): # 定義啟動IE瀏覽器的方法

print("starting ie browser now! %s" %ctime())

ie_driver=webdriver.Ie()

ie_driver.get("http://www.baidu.com")

sleep(5)

ie_driver.quit()

threading_list=[] #創(chuàng)建一個空列表用于存儲線程

# 定義一個執(zhí)行start_chrome()方法的線程

chrome_thread= threading.Thread(target=start_chrome)

threading_list.append(chrome_thread) # 將線程放到列表中

# 定義一個執(zhí)行start_firefox()方法的線程

firefox_thread= threading.Thread(target=start_firefox)

threading_list.append(firefox_thread) # 將線程放到列表中'''# 定義一個執(zhí)行start_chrome()方法的線程

ie_thread= threading.Thread(target=start_ie)

threading_list.append(ie_thread) # 將線程放到列表中'''

if __name__ == '__main__':for start_thread inthreading_list:

start_thread.start()for start_thread inthreading_list:

start_thread.join()

print(u"全部結(jié)束 %s" % ctime())

————————————————

版權(quán)聲明:本文為CSDN博主「Davieyang.D.Y」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/dawei_yang000000/article/details/105609689

總結(jié)

以上是生活随笔為你收集整理的python如何开启多线程_Python如何创建多线程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。