from asyncio import Queue,sleep
import asyncio
from threading import Thread
import time qu=Queue()
#put async def putQ():
global qu
print('start put')
i=1
while True:
print('put sleep')
await sleep(1)
print('put sleep')
await qu.put(i)
if i<=20:
print('put',i)
else:
break
i+=1 #get
async def getQ():
global qu
while True:
await sleep(0.1)
if qu.qsize() != 0:
a= await qu.get()
print('get',a)
else:
print('no get') def start_loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever() new_loop=asyncio.new_event_loop()
t=Thread(target=start_loop,args=(new_loop,))
t.start() # t1=asyncio.create_task(putQ()) taskput=asyncio.run_coroutine_threadsafe(putQ(),loop=new_loop)
taskget=asyncio.run_coroutine_threadsafe(getQ(),loop=new_loop) while True:
time.sleep(1)

 

from asyncio import Queue,sleep
import asyncio
from threading import Thread
import time qu=Queue()
#put async def putQ(qu):
print('start put')
i=1
while True:
print('put sleep')
await sleep(1)
print('put sleep')
await qu.put(i)
if i<=20:
print('put',i)
else:
break
i+=1 #get
async def getQ(qu):
while True:
await sleep(0.1)
if qu.qsize() != 0:
a= await qu.get()
print('get',a)
else:
print('no get') def start_loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever() new_loop=asyncio.new_event_loop()
t=Thread(target=start_loop,args=(new_loop,))
t.start() # t1=asyncio.create_task(putQ()) taskput=asyncio.run_coroutine_threadsafe(putQ(qu),loop=new_loop)
taskget=asyncio.run_coroutine_threadsafe(getQ(qu),loop=new_loop) while True:
time.sleep(1)

  

 

asyncio queue的更多相关文章

  1. asyncio Queue的使用例子

    import aiohttp import asyncio import async_timeout from urllib.parse import urljoin, urldefrag root_ ...

  2. python中利用队列asyncio.Queue进行通讯详解

    python中利用队列asyncio.Queue进行通讯详解 本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细 ...

  3. python异步编程模块asyncio学习(二)

    尽管asyncio应用通常作为单线程运行,不过仍被构建为并发应用.由于I/O以及其他外部事件的延迟和中断,每个协程或任务可能按一种不可预知的顺序执行.为了支持安全的并发执行,asyncio包含了thr ...

  4. asyncio标准库7 Producer/consumer

    使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...

  5. 深入Asyncio(二)从线程到协程

    线程的真相 多线程并不是一无是处,在实际问题中,要权衡优劣势来选择多线程.多进程或是协程.协程为多线程的某些问题提供了一种解决方案,所以学习协程首先要对线程有一定了解. 多线程优点 代码可读性 多线程 ...

  6. 深入Asyncio(三)Asyncio初体验

    Asyncio初体验 Asyncio在Python中提供的API很复杂,其旨在替不同群体的人解决不同的问题,也正是由于这个原因,所以很难区分重点. 可以根据asyncio在Python中的特性,将其划 ...

  7. asyncio:python3未来并发编程主流、充满野心的模块

    介绍 asyncio是Python在3.5中正式引入的标准库,这是Python未来的并发编程的主流,非常重要的一个模块.有一个web框架叫sanic,就是基于asyncio,语法和flask类似,使用 ...

  8. Python之asyncio模块的使用

    asyncio模块作用:构建协程并发应用的工具 python并发的三大内置模块,简单认识: .multiprocessing:多进程并发处理 .threading模块:多线程并发处理 .asyncio ...

  9. asyncio异步模块的21个协程编写实例

    启动一个无返回值协程 通过async关键字定义一个协程 import sys import asyncio async def coroutine(): print('运行协程') if sys.ve ...

随机推荐

  1. 281A

    #include <iostream> #include <string> #include <cctype> using namespace std; int m ...

  2. centos7下git服务器端搭建

    git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 Git,只能下载源码进行安装. 1. 查 ...

  3. 远程访问对象java实现

    服务端: 定义远程接口: package com.my.rmi; import java.rmi.Remote; import java.rmi.RemoteException; public int ...

  4. [LeetCode] 586. Customer Placing the Largest Number of Orders_Easy tag;SQL

    Query the customer_number from the orders table for the customer who has placed the largest number o ...

  5. ERROR [main] zookeeper.RecoverableZooKeeper: ZooKeeper create failed after 4 attempts

    ERROR [main] zookeeper.RecoverableZooKeeper: ZooKeeper create failed after 4 attempts ERROR [main] m ...

  6. android 调用webview控件,为逆向h5app做准备

    activity对应layout文件加入: <WebView android:id="@+id/main_web" android:layout_width="ma ...

  7. 有关于异常捕获点滴,plus我也揭揭java的短

    ▄︻┻┳═一『异常捕获系列』Agenda: ▄︻┻┳═一有关于异常捕获点滴,plus我也揭揭java的短 ▄︻┻┳═一根据异常自定义处理逻辑([附]java异常处理规范) ▄︻┻┳═一利用自定义异常来 ...

  8. unity3d-游戏实战突出重围,第三天 绘制数字

    实现效果: 准备资源 using UnityEngine; using System.Collections; public class hznum : MonoBehaviour { //存储图片资 ...

  9. Spring框架第一天

    ## 今天课程:Spring框架第一天 ## ---------- **Spring框架的学习路线** 1. Spring第一天:Spring的IOC容器之XML的方式,Spring框架与Web项目整 ...

  10. Redis Cluster(集群)的搭建

    一.Redis的下载.安装.启动(单实例) 我们统一将Redis安装在/opt目录下,执行命令如下: $ cd /opt $ wget http://download.redis.io/release ...