多线程+queue

两种定义线程方法

1调用threading.Thread(target=目标函数,args=(目标函数的传输内容))(简洁方便)

2创建一个类继承与(threading.Thread)并重构run()函数

class MyThread(threading.Thread):
def run(self): 1.直接调用函数方法实例
import threading,time

def run(n):
print("test",n)
time.sleep(2)
start_time=time.time()
tall=[] #用作储存线程实例
for i in range(50):
t=threading.Thread(target=run,args=('t-%s'%i,))
t.setDaemon(True) #把当前线程设置为守护线程
t.start()
tall.append(t) # for t in tall:
# t.join()
print("time:",time.time()-start_time)
print(threading.active_count())

2.①继承类 ②实例化 示例

import threading,time
class MyThread(threading.Thread):
def __init__(self,n):
super(MyThread,self).__init__()
self.n=n
def run(self):
print("running task",self.n) t1=MyThread("t1")
t2=MyThread("t2")
t1.start()
t2.start()

补充:

1两个进程一起进行 线程.join()等待该线程执行完毕后 在进行之后操作

2整个程序有主线程,会和其他线程并行

3可将线程储存在数组里

4  t.setDaemon(True)  #把当前线程设置为守护线程-主线程结束守护线程自动结束(主仆关系)

5 t.start()调用start函数代表线程开始

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

同步对象 event

可调用的函数
#event.wait() 使当前线程等待 直到被设定
#event.set() 设定标志位
#event.is_set() 判断是否被设定
#event.clear() 清除设定
以下是对红绿灯的示例
灯为一线程
车为一线程
import time
import threading
event=threading.Event()
def lighter():
count=0
event.set()
while True:
time.sleep(1)
if count>4 and count<10: #改为红灯
event.clear()
print("\033[41;1mred light is on...\033[0m")
elif count>=10:
event.set()
count=0
else:
print("\033[42;1mgreen light is on...\033[0m")
time.sleep(1)
count+=1
def car(name):
while True:
if event.is_set():
print(name,"is going")
time.sleep(1)
else:
print(name,"stop!")
event.wait()
car1=threading.Thread(target=car,args=("宝马",))
car1.start()
t=threading.Thread(target=lighter,)
t.start()

注意:

1先实例化event对象 event=threading.Event()

2利用上方红字4个event函数调用 达到多线程交互进行

3threading.Thread(target=car,args=("宝马",)) 在实例化线程时threading.Thread(target=调用的函数名,args=(参数,))  //必须使用元组形式

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

queue队列

q=queue.Queue(maxsize=5) 首先实例化队列 可以自定最大值需要maxsize=

1q.put()  //向队列中塞一个

2q.get()  //从队列中取一个

最多塞maxsize个 最少为0个 取一个后,此数据将不存在

以下是对queue的实例

提供者与两个消费者三线程

提供者最多产5个

import queue,threading,time
q=queue.Queue(maxsize=5)
def produce(name):
count=0
while True:
count+=1
# print(count)
q.put("%s生产%s个面包"%(name,count))
print("产了",count)
time.sleep(0.2)
def consumer(name):
while True:
print(name,"is eating ",q.get())
time.sleep(1)
a=threading.Thread(target=produce,args=("cf",))
b=threading.Thread(target=consumer,args=("xxx",))
c=threading.Thread(target=consumer,args=("sss",))
a.start()
b.start()
c.start()

注意:

q=queue.Queue(maxsize=5)务必使用前实例化!!

day11学python 多线程+queue的更多相关文章

  1. 一起学Python: 多线程-共享全局变量问题

    多线程-共享全局变量问题 多线程开发可能遇到的问题 假设两个线程t1和t2都要对全局变量g_num(默认是0)进行加1运算,t1和t2都各对g_num加10次,g_num的最终的结果应该为20. 但是 ...

  2. 一起学Python:多线程-共享全局变量

    多线程-共享全局变量 from threading import Thread import time g_num = 100 def work1(): global g_num for i in r ...

  3. C语言老司机学Python (六)- 多线程

    前面的1-5都是比较基础的东西,能做的事情也有限. 从本节起,随着更多进阶技术的掌握,渐渐就可以用Python开始浪了. Python3使用threading模块来实现线程操作. 根据在其他语言处学来 ...

  4. Python中Queue模块及多线程使用

    Python的Queue模块提供一种适用于多线程编程的FIFO实现.它可用于在生产者(producer)和消费者(consumer)之间线程安全(thread-safe)地传递消息或其它数据,因此多个 ...

  5. python多线程--优先级队列(Queue)

    Python的Queue模块中提供了同步的.线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue.这些队列都实现 ...

  6. 用Queue控制python多线程并发数量

    python多线程如果不进行并发数量控制,在启动线程数量多到一定程度后,会造成线程无法启动的错误. 下面介绍用Queue控制多线程并发数量的方法(python3). # -*- coding: utf ...

  7. python基础-12 多线程queue 线程交互event 线程锁 自定义线程池 进程 进程锁 进程池 进程交互数据资源共享

    Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...

  8. 【跟我一起学Python吧】Python 多线程

    其实自我感觉Python的多线程很类似于Java的多线程机制,但是比JAVA的多线程更灵活.在早期的Python多线程实现中,采用了thread模块.例如: from time import ctim ...

  9. D10——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D10 20180906内容纲要: 1.协程 (1)yield (2)greenlet (3)gevent (4)gevent实现单线程下socket多并发 2. ...

随机推荐

  1. avalon1.3的新特性预览

    avalon1.2的性能优化风暴很快就告一段落,入职也快一个月了,许多乱七八糟的事也少了下来,估计未来一个月会有许多好东呈现给大家. 首先是一个性能检测工具.由于MVVM是将原本由人脑干的事,转到各种 ...

  2. Using Mono DLLs in a Unity Project

    [Using Mono DLLs in a Unity Project] The path to the Unity DLLs will typically be: 一个生成dll的例子如下: mcs ...

  3. Apache Hive (一)Hive初识

    转自:https://www.cnblogs.com/qingyunzong/p/8707885.html Hive 简介 什么是Hive 1.Hive 由 Facebook 实现并开源 2.是基于 ...

  4. Spark scala和java的api使用

    1.利用scala语言开发spark的worcount程序(本地运行) package com.zy.spark import org.apache.spark.rdd.RDD import org. ...

  5. solr的客户端操作:使用solrj进行curd操作

    导入相关的jar包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-s ...

  6. css实现图标移上图标弹跳效果

    html部分: <div class="bounce" style="width:20px;height:20px;border:1px solid red;&qu ...

  7. 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值

    [抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  8. vmware Selinux配置错误,导致无法启动虚拟机

    Linux 开机提示kernel panic - not syncing: Attempted to kill init! 解决方法: 系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜 ...

  9. SUSE Linux 多路径软件+LVM+裸设备的配置

    1.先要查出光纤卡的WWN号,SUSE 10下光纤卡的WWD在/sys/class/fc_host/host5/port_name文件中,有两块卡就会有两个host*的目录 接好光纤后可以在dev下可 ...

  10. c++ std::unordered_set

    std::unordered_set template < class Key, // unordered_set::key_type/value_type class Hash = hash& ...