python多线程信息提示
生活随笔
收集整理的這篇文章主要介紹了
python多线程信息提示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
時間提醒
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標識的線程,當程序結束的時候,線程強制結束
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")結束子線程后結束主線程
使用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() # 加上這一句會強制等待所有子線程結束后結束主線程logging.info("Main : all done")啟動多線程的方法
ThreadPoolExecutor頭文件
import concurrent.futures實際調用
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:executor.map(thread_function, range(3))加鎖(包含對with 語句的理解)
生產者消費者線程
快的是生產者(要理發的人),慢的是消費者(理發師)
生產者排隊,將一個隨機信息發給消費者
多進程讀寫鎖
寫的時候不能有人讀或者寫,讀的時候沒人寫就行
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)總結
以上是生活随笔為你收集整理的python多线程信息提示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法笔记实战 模拟
- 下一篇: python变长参数传递