美文网首页
Python中的多线程

Python中的多线程

作者: 小橙子_43db | 来源:发表于2020-01-09 13:03 被阅读0次

Python3 线程中常用的两个模块为:

_thread

threading(推荐使用)

thread 模块已被废弃。用户可以使用 threading 模块代替。所以,在 Python3 中不能再使用"thread" 模块。为了兼容性,Python3 将 thread 重命名为 "_thread"。

函数时调用:

最简单的读线程案例:

#threading模块创建线程实例

#步骤:1.用Thread类创建一个实例 2.实例化后调用start方法启动线程

from threading import Thread

#线程函数

def threadDemo(msg):

    print(msg)

if __name__ == '__main__':

    #实例化线程类,target参数为线程执行的函数,args参数为线程函数的参数

    thread1 = Thread(target=threadDemo,args=('this is a thread demo',))

    thread1.start()

输出:this is a thread demo

继承式调用

#步骤:1.继承Thread类创建一个新的子类 2.实例化后调用start方法启动线程

#线程函数

def threadDemo(msg):

    print(msg)

class MyThread(Thread):

    def __init__(self,msg):

        super(MyThread,self).__init__()

        self.msg = msg

    def run(self):

        threadDemo(self.msg)

if __name__ == '__main__':

    t1 = MyThread('thread t1')

    t2 = MyThread('thread t2')

    t1.start()

    t2.start()

    t1.join()

    t2.join()

输出:thread t1

           thread t2

线程同步

线程在运行时是相互独立的,线程与线程间互不影响

def print_time(threadName,count,delay):

    while count:

        time.sleep(delay)

        print("%s: %s" % (threadName, time.ctime(time.time())))

        count -= 1

class MyThread(Thread):

    def __init__(self,name,count):

        super(MyThread,self).__init__()

        self.name = name

        self.count = count

    def run(self):

        print('线程开始'+self.name)

        print_time(self.name,self.count,2)

        print('线程结束'+self.name)

if __name__ == '__main__':

    th1 = MyThread('th1',5)

    th2 = MyThread('th2',5)

    th1.start()

    th2.start()

    th1.join()

    th2.join()

    print('--主线程结束--')

输出结果:

线程开始th1

线程开始th2

th1: Tue Dec 31 10:25:30 2019

th2: Tue Dec 31 10:25:30 2019

th1: Tue Dec 31 10:25:32 2019

th2: Tue Dec 31 10:25:32 2019

th1: Tue Dec 31 10:25:34 2019

th2: Tue Dec 31 10:25:34 2019

th1: Tue Dec 31 10:25:36 2019

th2: Tue Dec 31 10:25:36 2019

th1: Tue Dec 31 10:25:38 2019

线程结束th1

th2: Tue Dec 31 10:25:38 2019

线程结束th2

--主线程结束--

相关文章

  • 线程、进程

    多线程 在介绍Python中的线程之前,先明确一个问题,Python中的多线程是假的多线程!为什么这么说,我们先明...

  • python爬虫--day05

    多线程 在介绍Python中的线程之前,先明确一个问题,Python中的多线程是假的多线程!为什么这么说,我们先明...

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

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

  • 浅析python的GIL

    Python中的GIL锁 在Python中,可以通过多进程、多线程和多协程来实现多任务。 在多线程的实现过程中,为...

  • Python-线程、线程池

    1. Python多线程 python3中常用的线程模块为:_thread(Python2中的thread)、th...

  • GIL 全局解释器锁

    GIL面试题如下 描述Python GIL概念,以及它对Python多线程的影响?在一个多线程抓取网页的程序中,多...

  • Python多线程变量优化—threadLocal

    Python多线程变量优化—threadLocal 再多线程的环境下,每个线程都有自己的数据。在多线程编程中应该尽...

  • python爬虫--day06

    进程 进程的概念 python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python...

  • 07-多线程和协程

    在介绍Python中的线程之前,先明确一个问题,Python中的多线程是假的多线程!为什么这么说,我们先明确一个概...

  • 浅谈python中的多线程和多进程(二)

    原创:hxj7 本文继续分享一个关于python多线程和多进程区别的例子 前文《浅谈python中的多线程和多进程...

网友评论

      本文标题:Python中的多线程

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