python 多线程就这么简单

python 多线程就这么简单理解python的多线程如此简单

大家好,又见面了,我是你们的朋友全栈君。

 

  多线程和多进程是什么自行google补脑

  对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂。所以,这里力图用简单的例子,让你对多线程有个初步的认识。

 

单线程

 

  在好些年前的MS-DOS时代,操作系统处理问题都是单任务的,我想做听音乐和看电影两件事儿,那么一定要先排一下顺序。

(好吧!我们不纠结在DOS时代是否有听音乐和看影的应用。^_^

from time import ctime,sleep def music(): for i in range(2): print "I was listening to music. %s" %ctime() sleep(1) def move(): for i in range(2): print "I was at the movies! %s" %ctime() sleep(5) if __name__ == '__main__': music() move() print "all over %s" %ctime()

   我们先听了一首音乐,通过for循环来控制音乐的播放了两次,每首音乐播放需要1秒钟,sleep()来控制音乐播放的时长。接着我们又看了一场电影,

每一场电影需要5秒钟,因为太好看了,所以我也通过for循环看两遍。在整个休闲娱乐活动结束后,我通过

print "all over %s" %ctime()

看了一下当前时间,差不多该睡觉了。

运行结果:

>>=========================== RESTART ================================
>>> I was listening to music. Thu Apr 17 10:47:08 2014 I was listening to music. Thu Apr 17 10:47:09 2014 I was at the movies! Thu Apr 17 10:47:10 2014 I was at the movies! Thu Apr 17 10:47:15 2014 all over Thu Apr 17 10:47:20 2014

  

  其实,music()和move()更应该被看作是音乐和视频播放器,至于要播放什么歌曲和视频应该由我们使用时决定。所以,我们对上面代码做了改造:

#coding=utf-8
import threading from time import ctime,sleep def music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(1) def move(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(5) if __name__ == '__main__': music(u'爱情买卖') move(u'阿凡达') print "all over %s" %ctime()

  对music()和move()进行了传参处理。体验中国经典歌曲和欧美大片文化。

运行结果:

>>> ======================== RESTART ================================
>>> I was listening to 爱情买卖. Thu Apr 17 11:48:59 2014 I was listening to 爱情买卖. Thu Apr 17 11:49:00 2014 I was at the 阿凡达! Thu Apr 17 11:49:01 2014 I was at the 阿凡达! Thu Apr 17 11:49:06 2014 all over Thu Apr 17 11:49:11 2014

 

 

 

多线程

 

  科技在发展,时代在进步,我们的CPU也越来越快,CPU抱怨,P大点事儿占了我一定的时间,其实我同时干多个活都没问题的;于是,操作系统就进入了多任务时代。我们听着音乐吃着火锅的不在是梦想。

  python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费你和时间,所以我们直接学习threading 就可以了。

继续对上面的例子进行改造,引入threadring来同时播放音乐和视频:

#coding=utf-8
import threading from time import ctime,sleep def music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(1) def move(func): for i in range(2): print "I was at the %s! %s" %(func,ctime()) sleep(5) threads = [] t1 = threading.Thread(target=music,args=(u'爱情买卖',)) threads.append(t1) t2 = threading.Thread(target=move,args=(u'阿凡达',)) threads.append(t2) if __name__ == '__main__': for t in threads: t.setDaemon(True) t.start() print "all over %s" %ctime()

 

import threading

首先导入threading 模块,这是使用多线程的前提。

 

threads = []

t1 = threading.Thread(target=music,args=(u’爱情买卖’,))

threads.append(t1)

  创建了threads数组,创建线程t1,使用threading.Thread()方法,在这个方法中调用music方法target=music,args方法对music进行传参。 把创建好的线程t1装到threads数组中。

  接着以同样的方式创建线程t2,并把t2也装到threads数组。

 

for t in threads:

  t.setDaemon(True)

  t.start()

最后通过for循环遍历数组。(数组被装载了t1和t2两个线程)

 

setDaemon()

  setDaemon(True)将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。子线程启动后,父线程也继续执行下去,当父线程执行完最后一条语句print “all over %s” %ctime()后,没有等待子线程,直接就退出了,同时子线程也一同结束。

 

start()

开始线程活动。

 

运行结果:

>>> ========================= RESTART ================================
>>> I was listening to 爱情买卖. Thu Apr 17 12:51:45 2014 I was at the 阿凡达! Thu Apr 17 12:51:45 2014  all over Thu Apr 17 12:51:45 2014

  从执行结果来看,子线程(muisc 、move )和主线程(print “all over %s” %ctime())都是同一时间启动,但由于主线程执行完结束,所以导致子线程也终止。 

 

继续调整程序:

 

...
if __name__ == '__main__':
    for t in threads:
        t.setDaemon(True)
        t.start()
    
    for t in threads:
         t.join() 

    print "all over %s" %ctime()

 

     我们只对上面的程序加了个join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。

  注意:  join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。

运行结果:

>>> ========================= RESTART ================================
>>> 
I was listening to 爱情买卖. Thu Apr 17 13:04:11 2014  I was at the 阿凡达! Thu Apr 17 13:04:11 2014

I was listening to 爱情买卖. Thu Apr 17 13:04:12 2014
I was at the 阿凡达! Thu Apr 17 13:04:16 2014
all over Thu Apr 17 13:04:21 2014

  从执行结果可看到,music 和move 是同时启动的。

  开始时间4分11秒,直到调用主进程为4分22秒,总耗时为10秒。从单线程时减少了2秒,我们可以把music的sleep()的时间调整为4秒。

... def music(func): for i in range(2): print "I was listening to %s. %s" %(func,ctime()) sleep(4) ...

执行结果:

>>> ====================== RESTART ================================
>>> I was listening to 爱情买卖. Thu Apr 17 13:11:27 2014I was at the 阿凡达! Thu Apr 17 13:11:27 2014 I was listening to 爱情买卖. Thu Apr 17 13:11:31 2014 I was at the 阿凡达! Thu Apr 17 13:11:32 2014 all over Thu Apr 17 13:11:37 2014

  子线程启动11分27秒,主线程运行11分37秒。

  虽然music每首歌曲从1秒延长到了4 ,但通多程线的方式运行脚本,总的时间没变化。

 

本文从感性上让你快速理解python多线程的使用,更详细的使用请参考其它文档或资料。

 ==========================================================

class threading.Thread()说明:

 

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

This constructor should always be called with keyword arguments. Arguments are:

  group should be None; reserved for future extension when a ThreadGroup class is implemented.

  target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

  name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

  args is the argument tuple for the target invocation. Defaults to ().

  kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing 

anything else to the thread.

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155862.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月3日 上午6:46
下一篇 2022年7月3日 上午6:46


相关推荐

  • Idea2020创建javaweb项目-图文

    Idea2020创建javaweb项目-图文选择在新窗口打开看到以下结果接下来将当前项目修改为web项目点击下方应用,创建web目录及web.xml文件开始编写代码,第一步导入jar包然后将需要的jar包复制到lib目录下,复制完成后,右键lib目录选择AddasLibrary….接下来就是创建包创建类以及页面,src选择右键创建packages及选择包右键选择javaclass创建类然后tomcat运行如果…

    2025年11月30日
    8
  • GLM-4.7-Flash从零开始:Dify平台接入GLM-4.7-Flash模型教程

    GLM-4.7-Flash从零开始:Dify平台接入GLM-4.7-Flash模型教程

    2026年3月12日
    2
  • 2021年Java后端开发学习路线(建议收藏!)

    2021年Java后端开发学习路线(建议收藏!)2021 的 Java 后端开发的学习路线欢迎使用 Markdown 编辑器大家好 这是你第一次写 CSDN 的博客 下面我为大家来介绍一下 2021 年学习 Java 的路线 让初学者少走弯路跟着路线走 一定可以找到心仪的 offer 还请大家不吝赐教 学习路线大致分为十部分和一个扩展 第一部分 Java 基础 Java 基础部分 初学者在初学 Java 时一定不要好高骛远 认认真真打好基础才行 虽然基础部分有点枯燥无味但是选择了就得坚持下去 罗马并非一日建成 如今基础课程娱乐混杂 选对精品很重要 B 站上很多视频 大家可以去试听

    2026年3月20日
    3
  • c语言头文件cstring,头文件 <string.h> <cstring> <string> 区别

    c语言头文件cstring,头文件 <string.h> <cstring> <string> 区别/**@filecstring*ThisisaStandardC++Libraryfile.Youshould@c\#includethisfile*inyourprograms,ratherthananyofthe@a*.himplementationfiles.**ThisistheC++versionoftheSt…

    2025年11月3日
    6
  • Spring Cloud原理解析

    Spring Cloud原理解析本文为转载文章,作者:中华石杉,十余年BAT架构经验,倾囊相授。作者微信公众号:石杉的架构笔记(ID:shishan100)概述毫无疑问,SpringCloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术。不过大多数讲解还停留在对SpringCloud功能使用的层面,其底层的很多原理,很多人可能并不知晓。因此本文将通过大量的手绘图,给大家谈谈SpringCloud微…

    2025年10月1日
    4
  • Windows 10版本business_editions和consumer_editions的区别?「建议收藏」

    Windows 10版本business_editions和consumer_editions的区别?「建议收藏」Windows10版本business_editions和consumer_editions的区别?【答1】二者都内置专业版,不同之处在于:consumer_editions版本包含:Home(家庭版);Education(教育版);Professional(专业版);business_editions版本包含:Education(教育版);Enterprise(企业版);Pro…

    2025年7月9日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号