美文网首页
python——多线程

python——多线程

作者: Jalynn葸 | 来源:发表于2018-05-16 15:53 被阅读17次
多线程-threading
import  time
from threading import Thread
#如果多个线程执行的是同一个函数的话,各自之间不会有影响
def test():
    print("哈哈哈哈哈哈哈")
    time.sleep(1)

for i in range(5):
    t = Thread(target = test)
    t.start()
哈哈哈哈哈哈哈
哈哈哈哈哈哈哈
哈哈哈哈哈哈哈
哈哈哈哈哈哈哈
哈哈哈哈哈哈哈

子类完成创建多线程

import  time
import threading
class MyThread(threading.Thread):
    def run(self):
        for i in range(3):
            time.sleep(3)
            msg = "I am "+self.name +'@'+str(i)
            print(msg)
        
if __name__ == '__main__':
    t = MyThread()
    t.start()
I am Thread-1@0
I am Thread-1@1
I am Thread-1@2
image.png

线程的执行顺序也是主线程和各个子线程随机执行,顺序不确定

线程对全局变量的修改

线程之间是共享全局变量的,进程间不共享变量,可以加一个flag来避免全局变量被修改。

使用互斥锁避免全局变量被修改

两个线程同时抢着上锁,如果有1方成功的上锁,那么导致另外一方会堵塞,一直等待这个锁被解开为止。所以叫互斥锁。
一般加锁会导致运行效率变低,所以,只在必要的地方加锁。以提高效率

#创建锁
mutex = threading.Lock()
#上锁
mutex.acquire()
#释放锁
mutex.release()

线程之间不共享全局变量

from threading import Thread
import  time
import threading

def test1():
    name = threading.current_thread().name
    print("thread name is %s"%name)
    g_num = 100
    if name == "Thread-1":
        g_num += 1
    else:
        time.sleep(2)
    print("thread name is %s----g_num=%d"%(name,g_num))

p1 = Thread(target=test1)
p1.start()
p2 = Thread(target=test1)
p2.start()
____________________________________________________
thread name is Thread-1
thread name is Thread-1----g_num=101
thread name is Thread-2
thread name is Thread-2----g_num=100
死锁

要尽量避免死锁
设置超时

生产者与消费者模式

队列就是给生产者和消费者解耦用

import threading
import time
from queue import Queue

class Producer(threading.Thread):
    def run(self):
        global queue
        count = 0
        while True:
            if queue.qsize() < 1000:
                for i in range(100):
                    count = count +1
                    msg = '生成产品'+str(count)
                    queue.put(msg)
                    print(msg)
            time.sleep(0.5)
 
class Consumer(threading.Thread):
    def run(self):
        global queue
        while True:
            if queue.qsize() > 100:
                for i in range(3):
                    msg = self.name + '消费了 '+queue.get()
                    print(msg)
            time.sleep(1)
 
 
if __name__ == '__main__':
    queue = Queue()
 
    for i in range(500):
        queue.put('初始产品'+str(i))
    for i in range(2):
        p = Producer()
        p.start()
    for i in range(5):
        c = Consumer()
        c.start()
异步
GIL问题(全局解释器锁)

python中多进程的效率远远大于多线程

相关文章

  • GIL

    谈谈python的GIL、多线程、多进程 最近在看 Python 的多线程,经常我们会听到老手说:“python下...

  • Python多线程编程——多线程编程中的加锁机制

    如果大家对Python中的多线程编程不是很了解,推荐大家阅读之前的两篇文章:Python多线程编程——多线程基础介...

  • 5-线程(补充)

    Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了...

  • Python_提高

    GIL全局解释器锁 描述Python GIL的概念, 以及它对python多线程的影响?编写⼀个 多线程抓取⽹⻚的...

  • Python程序员都知道的入门知识の八

    目录【Python程序员都知道的入门知识】 1. 多线程threading、Queue Python的多线程由th...

  • Python多线程实现生产者消费者

    1. Python多线程介绍 Python提供了两个有关多线程的标准库,thread和threading。thre...

  • 多线程

    Python多线程原理与实战 目的: (1)了解python线程执行原理 (2)掌握多线程编程与线程同步 (3)了...

  • Python多线程(上)

    前言 说起Python的多线程,很多人都嗤之以鼻,说Python的多线程是假的多线程,没有用,或者说不好用,那本次...

  • Python 3中的多线程

    Python 3的多线程 Python 3的多线程模块threading在旧版_thread模块基础上进行了更高层...

  • Python 多线程抓取图片效率实验

    Python 多线程抓取图片效率实验 实验目的: 是学习python 多线程的工作原理,及通过抓取400张图片这种...

网友评论

      本文标题:python——多线程

      本文链接:https://www.haomeiwen.com/subject/vrmpdftx.html