iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python threading
  • 765
分享到

Python threading

Pythonthreading 2023-01-31 07:01:44 765人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

 1.  第一种方式: 创建一个threading.Thread()的实例,给它一个函数。 import threading from time import sleep, ctime loops = [

 1.  第一种方式: 创建一个threading.Thread()的实例,给它一个函数。

import threading


from time import sleep, ctime

loops = [4, 2]

def loop(nloop, nsec):
    print '\nstart loop:', nloop, 'at:', ctime()
    sleep(nsec)
    print '\nloop', nloop, 'done at:', ctime()

def main():
    print '\nstarting at:\n', ctime()
    threads = []
    nloops = range(len(loops))

    #create the threads using threading
    for i in nloops:
         t = threading.Thread(target=loop, args=(i,loops[i]))
         threads.append(t)

    #start threads
    for i in nloops:
        threads[i].start()

    #wait for all
    for i in nloops:
        threads[i].join() #block
        
    print '\nall Done at:', ctime()

if __name__ == '__main__':
    main()

 

/-----------------------------------------------------------------------------------------------------------------------------------------------------

2. 第二种方式: 创建一个threading.Thread的实例,传给它一个可调用类对象,类中使用__call__()函数调用函数

import threading
from time import sleep, ctime

loops = [4,2]

class ThreadFunc(object):
    def __init__(self, func, args, name=''):
        self.name = name
        self.func = func
        self.args = args
    #when you create a new thread, Thread instance will invoke our ThreadFunc
    # instance, and at that time, it will call the function __call__()
    # Hence, we have a tuple arguments , so we use the self.res
    def __call__(self):
        self.res = self.func(*self.args) #invoke the func

def loop(nloop, nsec):
    print '\nstart loop:', nloop, 'at:', ctime()
    sleep(nsec)
    print '\nloop', nloop, 'done at:', ctime()

def main():
    print 'start at:', ctime()
    threads = [] #list
    nloops = range(len(loops))

    #crate all threads
    for i in nloops:
        t = threading.Thread(target=ThreadFunc(loop, (i, loops[i]),loop.__name__))
        threads.append(t)

    #start all threads
    for i in nloops:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all Done at:', ctime()

if __name__ == '__main__':
    main()


/-----------------------------------------------------------------------------------

3. 第三种方式: 派生一个threading.Thread出一个子类,创建这个子类的实例,使用run调用函数

import threading 
from time import sleep, ctime

loops = (4,2)

class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self) #base class func
        self.name = name
        self.func = func
        self.args = args

    # in the other way, use __call__()
    def run(self):
        apply(self.func,self.args)

def loop(nloop, nsec):
    print 'start loop', nloop, 'at:', ctime()
    sleep(nsec)
    print 'loop', nloop, 'done at:', ctime()

def main():
    print 'starting at:',ctime()
    threads = []
    nloops = range(len(loops))

    for i in nloops:
        t = MyThread(loop, (i,loops[i]), loop.__name__)
        threads.append(t)

    for i in nloops:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all Done at:', ctime()

if __name__ == '__main__':
    main()

--结束END--

本文标题: Python threading

本文链接: https://www.lsjlt.com/news/191363.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • [python][threading][
        thread不支持守护线程,当主线程退出时,所有的子线程无条件推出;针对这种情况,threading引入了守护线程的概念。     如果主线程要退出的时候,不用等待子线程完成,那就在线程开始之前,即调用start()之前,调用set...
    99+
    2023-01-31
    python threading
  • Python threading
     1.  第一种方式: 创建一个threading.Thread()的实例,给它一个函数。 import threading from time import sleep, ctime loops = [...
    99+
    2023-01-31
    Python threading
  • Threading in Python-
    原文是2.x版本的,然后应该是英文的.我在学习的过程中,同时改成python 3.3并且改成中文,引入一些自己的理解.Thread Objects线程对象The simplest way to use a Thread is to inst...
    99+
    2023-01-31
    Threading Python
  • Python中的threading
    #!/usr/bin/env python# -*- coding: utf-8 -*-import threading, time#新线程执行的代码:def loop():    print('thread %s is running.....
    99+
    2023-01-31
    Python threading
  • Python中threading的joi
    python的进程和线程经常用到,之前一直不明白threading的join和setDaemon的区别和用法,今天特地研究了一下。multiprocessing中也有这两个方法,同样适用,这里以threading的join和setDaem...
    99+
    2023-01-30
    Python threading joi
  • Python线程之threading
    线程是属于进程的,线程运行在进程空间内,同一进程所产生的线程共享同一内存空间,当进程退出时该进程所产生的线程都会被强制退出并清除。进程是资源分配的最小单位,线程是CPU调度的最小单位,每一个进程中至少有一个线程,线程可与属于同一进程的其它线...
    99+
    2023-01-31
    线程 Python threading
  • Python线程threading(Thread类)
    目录前言Python创建线程 threading调用Thread类的构造器创建线程继承Thread类创建线程类Thread join()用法前言 几乎所有的操作系统都支持同时运行多个...
    99+
    2024-04-02
  • python多线程threading
    本文通过 4个example 介绍python中多线程package —— threading的常用用法, 包括调用多线程, 同步队列类Queue, Ctrl+c结束多线程。 example1. 调用10个线程, 分别打印0~...
    99+
    2023-01-31
    多线程 python threading
  • python 通过threading多线
    #!/usr/bin/env python#coding=utf-8import paramikoimport time,datetime,threadingdef ssh(ip,user,passwd,command):    ssh =...
    99+
    2023-01-31
    python threading 多线
  • python threading多线程p
    #!/usr/bin/env python                                                                                       #_*_coding:u...
    99+
    2023-01-31
    多线程 python threading
  • python threading超线程使
    在工作过程中中,将内容过程中经常用的内容片段珍藏起来,下面内容段是关于python threading超线程使用简单范例的内容,希望能对小伙伴们有较大帮助。 # encoding: UTF-8 import threading # 方法1...
    99+
    2023-01-31
    超线程 python threading
  • Python 多线程threading模
            首先,我们在了解多线程时需要理解的就是什么是多线程,按照官方的解释就是:多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。    在我自学到这里的时候,通过会在想进程和线程到底是有...
    99+
    2023-01-31
    多线程 Python threading
  • Python多线程threading用法
    Python里面经常会用到多线程,即所有的方法在同一时间开始运行,而不是按顺序一个一 个运行。所用到的模块为threading,下面详解threading用法。 我们写三个方法,one、two、three并正常运行。 这里只截图了one()...
    99+
    2023-01-31
    多线程 Python threading
  • Python 多线程之 threading 模块
    在之前的文章中,我们已经介绍了 Python 通过 _thread 和 threading 模块提供了对多线程的支持,threading 模块兼具了 _thread 模块的现有功能,又扩展了一些新的功能,具有十分丰富的线程操作功能,本节我们...
    99+
    2023-09-16
    python 开发语言 java
  • Python中如何使用threading
    Python中如何使用threading,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。  Python 版本是3.7.4  前面的文章记录了网络请求(urllib,requ...
    99+
    2023-06-02
  • python 包之 threading 多线程
    目录一、创建一个线程二、创建多个线程三、线程同步四、递归锁五、信号锁一、创建一个线程 通过实例化threading.Thread类创建线程 import threading def...
    99+
    2024-04-02
  • python中threading模块如何使用
    本篇内容介绍了“python中threading模块如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、Thread的使用目标函数可以...
    99+
    2023-07-06
  • python中threading实现线程的示例分析
    小编给大家分享一下python中threading实现线程的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!过程说明从Thread类构成子类。覆盖方法根据需...
    99+
    2023-06-20
  • threading模块如何在python中应用
    threading模块如何在python中应用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。python有哪些常用库python常用的库:1.requesuts;2.scr...
    99+
    2023-06-14
  • python中threading模块怎么使用
    python中threading模块详解,threading提供了一个比thread模块更高层的API来提供线程的并发性。这些线程并发运行并共享内存。下面来看threading模块的具体用法:一、Thread的使用目标函数可以实例化一个Th...
    99+
    2023-05-15
    Python threading
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作