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

Python中的threading

Pythonthreading 2023-01-31 01:01:19 723人浏览 八月长安

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

摘要

#!/usr/bin/env python# -*- coding: utf-8 -*-import threading, time#新线程执行的代码:def loop():    print('thread %s is running..

#!/usr/bin/env python

# -*- coding: utf-8 -*-


import threading, time


#新线程执行的代码:

def loop():

    print('thread %s is running...' % threading.current_thread().name)

    n = 0

    while n < 5:

        n = n + 1

        print('thread %s ' % threading.current_thread().name)

        time.sleep(3)

    print('thread %s ended.' % threading.current_thread().name)


print('thread %s is running...' % threading.current_thread().name)

t = threading.Thread(target=loop, name='LoopThread')

t.start()

#t.join()

print('thread %s ended.' % threading.current_thread().name)


执行结果:

thread MainThread is running...

thread LoopThread is running...thread MainThread ended.


thread LoopThread

thread LoopThread

thread LoopThread

thread LoopThread

thread LoopThread

thread LoopThread ended.


会在主线程中创建子线程,主线程和子线程并行运行


#!/usr/bin/env Python

# -*- coding: utf-8 -*-


import Queue 

import time 

import threading 

import random 


def Go(name):

   # time.sleep(1)

    print("go" + str(name))

    time.sleep(1)


for j in range(20):

    t1 = threading.Thread(group=None, target=go, name=None, args=(j,)) 

    t1.start()


for i in range(20):

    go(i)


j是并行执行,主线程和20个子线并行执行

i是串行执行,只有主线程



#!/usr/bin/env python

# -*- coding: utf-8 -*-


import Queue 

import time 

import threading 

import random 



q = Queue.Queue()


def pro(name):

    print("---pro data kaiqi")

    #time.sleep(3)

    for i in range(20):

        time.sleep(1)

        print(name + "pro data" + str(i))

        q.put(i)


def con(name):

    print("-----con data kaiqi")

    n = 0

    while n < 20:

        time.sleep(3)

        data = q.get()

        print(name + "con data----" + str(data))

        n += 1


t1 = threading.Thread(group=None, target=pro, name=None, kwargs={'name':'laoban'}) 

t2 = threading.Thread(group=None, target=con, name=None, kwargs={'name':'yuangong'})

t1.start()

t2.start()      


两个线程并行交互,在一个队列中拿数据

--结束END--

本文标题: Python中的threading

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

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

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

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

下载Word文档
猜你喜欢
  • 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][
        thread不支持守护线程,当主线程退出时,所有的子线程无条件推出;针对这种情况,threading引入了守护线程的概念。     如果主线程要退出的时候,不用等待子线程完成,那就在线程开始之前,即调用start()之前,调用set...
    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
     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
    Python中如何使用threading,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。  Python 版本是3.7.4  前面的文章记录了网络请求(urllib,requ...
    99+
    2023-06-02
  • python中threading实现线程的示例分析
    小编给大家分享一下python中threading实现线程的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!过程说明从Thread类构成子类。覆盖方法根据需...
    99+
    2023-06-20
  • python中threading模块如何使用
    本篇内容介绍了“python中threading模块如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、Thread的使用目标函数可以...
    99+
    2023-07-06
  • 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
  • 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
  • 浅谈一下python中threading模块
    目录一、Thread的使用二、threading.activeCount()的使用三、threading.enumerate()的使用。四、threading.setDaemon()...
    99+
    2023-05-18
    python threading 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模块,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。python的数据类型有哪些python的数据类型:1. 数字类型,包...
    99+
    2023-06-14
  • Python多线程threading用法
    Python里面经常会用到多线程,即所有的方法在同一时间开始运行,而不是按顺序一个一 个运行。所用到的模块为threading,下面详解threading用法。 我们写三个方法,one、two、three并正常运行。 这里只截图了one()...
    99+
    2023-01-31
    多线程 Python threading
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作