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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python多线程信息提示

發(fā)布時間:2025/3/21 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python多线程信息提示 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

時間提醒

import threading import logging import timedef thread_function(name):logging.info("Thread %s starting", name)time.sleep(2000)logging.info("Thread %s finishing", name)if __name__ == "__main__":format = "%(asctime)s: %(message)s"logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")logging.info("Main : before creating thread")x = threading.Thread(target=thread_function, args=(1,))logging.info("Main : wait for the thread to finish")logging.info("Main : all done")

daemon

含有daemon標(biāo)識的線程,當(dāng)程序結(jié)束的時候,線程強制結(jié)束

import threading import logging import timedef thread_function(name):logging.info("Thread %s starting", name)time.sleep(2)logging.info("Thread %s finishing", name)if __name__ == "__main__":format = "%(asctime)s: %(message)s"logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")logging.info("Main : before creating thread")x = threading.Thread(target=thread_function, args=(1,), daemon=True)x.start()logging.info("Main : wait for the thread to finish")logging.info("Main : all done")

結(jié)束子線程后結(jié)束主線程

使用join

import threading import logging import timedef thread_function(name):logging.info("Thread %s starting", name)time.sleep(2)logging.info("Thread %s finishing", name)if __name__ == "__main__":format = "%(asctime)s: %(message)s"logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")logging.info("Main : before creating thread")x = threading.Thread(target=thread_function, args=(1,), daemon=True)x.start()logging.info("Main : wait for the thread to finish")x.join() # 加上這一句會強制等待所有子線程結(jié)束后結(jié)束主線程logging.info("Main : all done")

啟動多線程的方法

ThreadPoolExecutor頭文件

import concurrent.futures

實際調(diào)用

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:executor.map(thread_function, range(3))

加鎖(包含對with 語句的理解)

生產(chǎn)者消費者線程

快的是生產(chǎn)者(要理發(fā)的人),慢的是消費者(理發(fā)師)
生產(chǎn)者排隊,將一個隨機信息發(fā)給消費者

import logging import threading import concurrent.futures import time import randomSENTINEL = object()class Pipeline(object):def __init__(self):self.message = 0self.producer_lock = threading.Lock()self.consumer_lock = threading.Lock()self.consumer_lock.acquire()def get_message(self, name):logging.debug("%s: about to acquire getlock", name)self.consumer_lock.acquire()logging.debug("%s: have getlock", name)message = self.messagelogging.debug("%s: about to release setlock", name)self.producer_lock.release()logging.debug("%s: setlock released", name)return messagedef set_message(self, message, name):logging.debug("%s: about to acquire setlock", name)self.producer_lock.acquire()logging.debug("%s: have setlock", name)self.message = messagelogging.debug("%s: about to release getlock", name)self.consumer_lock.release()logging.debug("%s: have released getlock", name)def producer(pipeline):'''pretend we are getting a message from the network'''for index in range(10):message = random.randint(1, 101)logging.info("Producer got message: %s", message)pipeline.set_message(message, "Producer")# send a sentinel message to tell consumer we're donepipeline.set_message(SENTINEL, "Producer")def consumer(pipeline):message = 0while message is not SENTINEL:message = pipeline.get_message("Consumer")if message is not SENTINEL:logging.info("Consumer storing message %s", message)if __name__ == '__main__':format = "%(asctime)s: %(message)s"logging.getLogger().setLevel(logging.DEBUG)logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")pipeline = Pipeline()with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:executor.submit(producer, pipeline)executor.submit(consumer, pipeline)

多進(jìn)程讀寫鎖

寫的時候不能有人讀或者寫,讀的時候沒人寫就行

import logging import threading import concurrent.futures import time import randomSENTINEL = object()class Pipeline(object):def __init__(self):self.message = 0self.producer_lock = threading.Lock()self.consumer_lock = threading.Lock()def get_message(self, name):while True:if not self.producer_lock.locked():logging.debug("%s: no write, begin to read", name)self.consumer_lock.acquire()logging.debug("%s: have get readlock", name)message = self.messagelogging.debug("%s: have get the message, about to release readlock", name)self.consumer_lock.release()breakreturn messagedef set_message(self, message, name):while True:if not self.consumer_lock.locked() and not self.producer_lock.locked():logging.debug("%s: no write and read, begin to write", name)self.producer_lock.acquire()logging.debug("%s: get setlock", name)self.message = messagelogging.debug("%s: setover about to release setlock", name)self.producer_lock.release()breakreturndef producer(pipeline):'''pretend we are getting a message from the network'''for index in range(10):message = random.randint(1, 101)logging.info("Producer got message: %s", message)pipeline.set_message(message, "Producer")# send a sentinel message to tell consumer we're donepipeline.set_message(SENTINEL, "Producer")def consumer(pipeline):message = 0while message is not SENTINEL:message = pipeline.get_message("Consumer")if message is not SENTINEL:logging.info("Consumer storing message %s", message)if __name__ == '__main__':format = "%(asctime)s: %(message)s"logging.getLogger().setLevel(logging.DEBUG)logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")pipeline = Pipeline()with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:executor.submit(producer, pipeline)executor.submit(producer, pipeline)executor.submit(consumer, pipeline)

總結(jié)

以上是生活随笔為你收集整理的python多线程信息提示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产精品77777 | 亚洲熟女一区二区 | 亚洲欧洲一区 | 深夜福利国产精品 | 亚洲欧美另类在线观看 | 69av视频 | 伊人精品影院 | 曰韩精品| 91久久精品无码一区二区 | 少妇性xxxxxxxxx色武功 | 欧美日韩一区二区三区国产精品成人 | 中文字幕日韩欧美一区二区 | 原创少妇半推半就88av | 可以免费看污视频的网站 | 国产在线观看一区 | 国产乱码精品一区二三区蜜臂 | 91人人看 | 超碰2| caoporn超碰97| 波多野结衣办公室双飞 | 美女露隐私网站 | 中文字幕一区二区三区人妻在线视频 | 国产成人手机在线 | 亚洲a在线观看 | 免费看的黄色小视频 | 婷婷色中文 | 亚洲中文字幕第一区 | 一级欧美一级日韩 | 亚洲国产精品va在线看黑人 | 加勒比在线免费视频 | 国产精品入口a级 | 女人夜夜春| 日韩欧美在线视频免费观看 | 日韩欧美中文字幕一区二区三区 | 久久久久久久极品 | 男女视频一区二区 | 欧美高清精品 | 热99精品| 欧美一级欧美三级 | 大尺度叫床戏做爰视频 | 69网站在线观看 | 天天插天天狠 | 一级黄色免费 | av最新网 | 欧美日韩国产免费观看 | √资源天堂中文在线 | 91黄色短视频 | 午夜三级福利 | 97人人澡| 亚洲六月婷婷 | 亚洲色偷精品一区二区三区 | 日韩三级免费看 | 国产三级一区 | 国产亚洲在线观看 | 图书馆的女友动漫在线观看 | 国产精品夜夜夜爽阿娇 | 亚洲AV无码精品久久一区二区 | 国产欧美日韩一区 | 欧美日韩一区二区视频在线观看 | 欧美18一20男同69gay | 亚洲一区二区在线免费观看 | 日本高清精品 | 午夜精品久久久久久久99 | 亚洲成年| 人人干网站 | 极品少妇一区 | 特级西西444www高清大胆 | 99国产精品久久久久久久成人 | 调教亲女小嫩苞h文小说 | 99热日本 | 欧美三级影院 | 久久综合色婷婷 | 国产第一精品 | 黄视频在线播放 | 精品午夜一区二区三区在线观看 | 国产一线在线观看 | 两个小y头稚嫩紧窄h文 | 男人天堂伊人 | 操mm影院 | 99re这里有精品 | 精品久久久亚洲 | 色先锋av | 亚洲v欧美v另类v综合v日韩v | 操一操 | 91香蕉国产在线观看软件 | 成人国产精品入口 | 就去吻综合 | 国内精品第一页 | 粉色午夜视频 | av香蕉 | 日日夜夜撸啊撸 | 日本午夜视频 | 丁香啪啪综合成人亚洲 | 色丁香综合 | 日本大尺度电影免费观看全集中文版 | 精品亚洲aⅴ无码一区二区三区 | 超碰2 | 麻豆精品视频 | 九九九免费 |