广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python多线程————3、多线程间通
  • 203
分享到

python多线程————3、多线程间通

多线程python 2023-01-31 07:01:06 203人浏览 安东尼

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

摘要

1、共享变量 #通过共享变量 import time import threading url_list = [] def get_detail_html(): global url_list while True:

1、共享变量


#通过共享变量
import time
import threading
url_list = []
def get_detail_html():
    global url_list
    while True:
        if len(url_list):
            url_list.pop()
            print("get detail html started")
            time.sleep(3)
            print("get detail html end")

def get_detail_url():
    while True:
        global url_list
        print("get detail url started")
        url_list.append(1)
        time.sleep(1)
        print("get detail url end")

if __name__ == "__main__":
    thread2 = threading.Thread(target=get_detail_url)
    thread2.start()
    for i in range(2):
        thread1 = threading.Thread(target=get_detail_html)
        thread1.start()
        #thread1.join()
    #thread1.setDaemon(True)
    #thread2.setDaemon(True)
    start_time = time.time()



    #thread2.join()
    print(time.time() - start_time)

 2、通过queue


#通过queue
import time
import threading
from queue import Queue
def get_detail_html(queue):
    while True:
            queue.put()
            print("get detail html started")
            time.sleep(3)
            print("get detail html end")

def get_detail_url(queue):
    while True:
        print("get detail url started")
        queue.get(1)
        time.sleep(1)
        print("get detail url end")

if __name__ == "__main__":
    url_queue = Queue(maxsize=1000)
    thread2 = threading.Thread(target=get_detail_url,args=(url_queue,))
    thread2.start()
    for i in range(2):
        thread1 = threading.Thread(target=get_detail_html,args=(url_queue,))
        thread1.start()
        #thread1.join()
    #thread1.setDaemon(True)
    #thread2.setDaemon(True)
    start_time = time.time()



    #thread2.join()
    print(time.time() - start_time)

 

--结束END--

本文标题: python多线程————3、多线程间通

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

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

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

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

下载Word文档
猜你喜欢
  • python多线程————3、多线程间通
    1、共享变量 #通过共享变量 import time import threading url_list = [] def get_detail_html(): global url_list while True: ...
    99+
    2023-01-31
    多线程 python
  • 多线程编程(3):线程池ThreadPo
    在面向对象编程中,经常会面对创建对象和销毁对象的情况,如果不正确处理的话,在短时间内创建大量对象然后执行简单处理之后又要销毁这些刚刚建立的对象,这是一个非常消耗性能的低效行为,所以很多面向对象语言中在内部使用对象池来处理这种情况,以提高性能...
    99+
    2023-01-31
    线程 多线程 ThreadPo
  • 自学多线程-3
    中断线程的运行: 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它。 代码示例如下: class Kirl implements Runnable  {      public void pri...
    99+
    2023-01-31
    多线程
  • Python 多线程
      文章来源:https://www.runoob.com/python/python-multithreading.html 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的...
    99+
    2023-01-31
    多线程 Python
  • python多线程
    Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理。 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的...
    99+
    2023-01-30
    多线程 python
  • python—多线程
    一、多线程实例  线程时应用程序中工作的最小单位,python中提供了threading模块来对多线程操作,一般多核cpu采用多进程方式,单核才采用多线程方式  方法:  将要执行的方法threading.Thread作为参数传给构造方法(...
    99+
    2023-01-31
    多线程 python
  • C#多线程之线程通讯(AutoResetEvent)
    一、简介 我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作。AutoResetEvent 允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访...
    99+
    2022-11-13
  • Python多线程编程,线程锁
    多线程threading 模块创建线程创建自己的线程类线程通信线程同步互斥方法线程锁@需要了解!!!   什么是线程? 线程也是一种多任务的编程方法,可以利用计算机多核资源完成程序的并发运行。 线程又被称为轻量级进程 ...
    99+
    2023-01-30
    线程 多线程 Python
  • python多线程socket编程--多
    Python中实现socket通信的服务端比较复杂,而客户端非常简单,所以客户端基本上都是用sockct模块实现,而服务 端用有很多模块可以使用,如下: 1、客户端 #!/usr/bin/env python #coding...
    99+
    2023-01-31
    多线程 python socket
  • C#多线程系列之线程通知
    AutoRestEvent 类用于从一个线程向另一个线程发送通知。 微软文档是这样介绍的:表示线程同步事件在一个等待线程释放后收到信号时自动重置。 其构造函数只有一个: 构造函数里面...
    99+
    2022-11-13
  • Python多线程编程
      一个串行程序需要从每个I/O终端通道来检测用户的输入,然而程序在读取过程中不能阻塞,因为用户输入的到达时间的不确定,并且阻塞会妨碍其他I/O通道的处理。由于串行程序只有唯一的执行线程,因此它需要兼顾执行的多个任务,确保其中的某个任务不会...
    99+
    2023-01-31
    多线程 Python
  • python 多线程编程
    使用回调方式 import time def countdown(n): while n > 0: print('T-minus', n) n -= 1 time.sleep...
    99+
    2023-01-31
    多线程 python
  • 彻底明白Java的多线程-线程间的通信(1)(转)
    彻底明白Java的多线程-线程间的通信(1)(转)[@more@]三. 线程间的通信1. 线程的几种状态线程有四种状态,任何一个线程肯定处于这四种状态中的一种:1) 产生(New):线程对象已经产生,但尚未被启动,所以无法执行。如通过new...
    99+
    2023-06-03
  • python多线程-Semaphore(
    Semaphore(value=1) Semaphore对象内部管理一个计数器,该计数器由每个acquire()调用递减,并由每个release()调用递增。计数器永远不会低于零,当acquire()发现计数器为零时,线程阻塞,等待其他线...
    99+
    2023-01-30
    多线程 python Semaphore
  • python 多线程+queue
    python的queue设计的是线程安全的,所以大家伙放心用吧! python多线程的一种简单的实现如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import threadi...
    99+
    2023-01-31
    多线程 python queue
  • python多线程threading
    本文通过 4个example 介绍python中多线程package —— threading的常用用法, 包括调用多线程, 同步队列类Queue, Ctrl+c结束多线程。 example1. 调用10个线程, 分别打印0~...
    99+
    2023-01-31
    多线程 python threading
  • Python 多线程 multithr
    【Python】python 多线程两种实现方式 目前python提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对threa...
    99+
    2023-01-31
    多线程 Python multithr
  • python之多线程
    一、threading 模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 二、开启线程的两种方式 from threading import Thread import ti...
    99+
    2023-01-30
    之多 线程 python
  • python threadpool多线程
            在写爬虫下载一个网页中的多个链接文件时(http://blog.sina.com.cn/s/blog_740773f40100ywyg.html  ),使用多线程会提高下载速度。         使用线程池能够简单的解决这...
    99+
    2023-01-31
    多线程 python threadpool
  • python多线程paramiko
    初学python,网上找发些关于paramiko实现python多线程的功能,发现相互抄袭占多.别人的总归是别人的,也同时为了练习技术,就自己写了一个基于paramiko免密认证多线程并发脚本.与大家共勉.使用上的问题的同学也可以联系我.刚...
    99+
    2023-01-31
    多线程 python paramiko
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作