日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。