asyncio queue
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的更多相关文章
- asyncio Queue的使用例子
import aiohttp import asyncio import async_timeout from urllib.parse import urljoin, urldefrag root_ ...
- python中利用队列asyncio.Queue进行通讯详解
python中利用队列asyncio.Queue进行通讯详解 本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细 ...
- python异步编程模块asyncio学习(二)
尽管asyncio应用通常作为单线程运行,不过仍被构建为并发应用.由于I/O以及其他外部事件的延迟和中断,每个协程或任务可能按一种不可预知的顺序执行.为了支持安全的并发执行,asyncio包含了thr ...
- asyncio标准库7 Producer/consumer
使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...
- 深入Asyncio(二)从线程到协程
线程的真相 多线程并不是一无是处,在实际问题中,要权衡优劣势来选择多线程.多进程或是协程.协程为多线程的某些问题提供了一种解决方案,所以学习协程首先要对线程有一定了解. 多线程优点 代码可读性 多线程 ...
- 深入Asyncio(三)Asyncio初体验
Asyncio初体验 Asyncio在Python中提供的API很复杂,其旨在替不同群体的人解决不同的问题,也正是由于这个原因,所以很难区分重点. 可以根据asyncio在Python中的特性,将其划 ...
- asyncio:python3未来并发编程主流、充满野心的模块
介绍 asyncio是Python在3.5中正式引入的标准库,这是Python未来的并发编程的主流,非常重要的一个模块.有一个web框架叫sanic,就是基于asyncio,语法和flask类似,使用 ...
- Python之asyncio模块的使用
asyncio模块作用:构建协程并发应用的工具 python并发的三大内置模块,简单认识: .multiprocessing:多进程并发处理 .threading模块:多线程并发处理 .asyncio ...
- asyncio异步模块的21个协程编写实例
启动一个无返回值协程 通过async关键字定义一个协程 import sys import asyncio async def coroutine(): print('运行协程') if sys.ve ...
随机推荐
- [django]django查询最佳实战
from django.db.models import Max, Min, Sum, Avg, Count, Q, F Django中的F和Q函数 一.F介绍 作用:操作数据表中的某列值,F()允许 ...
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- SQL Server2008 R2 数据库镜像实施手册(双机)SQL Server2014同样适用
这篇文章主要介绍了SQL Server2008 R2 数据库镜像实施手册(双机)SQL Server2014同样适用,需要的朋友可以参考下 一.配置主备机 1. 服务器基本信息 主机名称为:HOST_ ...
- js获取微信code
function callback(result) { alert('cucess'); alert(result); //输出openid } function getQueryString(nam ...
- Selenium基础知识(八)文本域处理textarea
textarea文本域selenium有时处理不了,但是我们可以采用迂回,解决这个问题 那么,我们用到了execute_script()方法来调用javascript来解决这个问题 这其中涉及DOM, ...
- UGUI之Slider使用,制作血条
用Slider来控制Cube旋转 Slider是滑动条.
- unity3d-知识汇总
itween下载 http://www.youkexueyuan.com/exp_show/1147.html 代码修改精灵图片的透明度 UIBp.GetComponent<Image>( ...
- mybatis--parametertype的参数传递
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- Android -- 实现RecyclerView可拖拽Item
1,今天和大家一起实现RecyclerView可拖拽Item,主要是使用RecyclerView结合ItemTouchHelper来实现的,来看一下效果 2,看一下怎么实现的呢,很简单,只需要给rec ...