python3.5.2怎么使用_如何在Python中使用聚集3.5.2

我试着学习如何在Python中使用asnychron编程,并编写了一个小型龙卷风应用程序,它执行两个带睡眠命令的asnyc循环。 如果我用两个await命令等待两个协程,它的行为与预期相同(第一个循环,比第二个循环执行的要快)。 如果我将两个协程与gather组合,则不会发生任何事情。 (没有错误,没有打印输出时,WebRequest的是永远不会结束。)如何在Python中使用聚集3.5.2

我不明白什么是与*发生伺机收集(COROS,return_exceptions = TRUE),

from asyncio import gather

import os.path

import tornado.ioloop

from tornado.options import define, options, parse_command_line

import tornado.web

import tornado.platform.asyncio

from tornado.gen import sleep

import datetime;

define("port", default=8888, help="run on the given port", type=int)

define("debug", default=False, help="run in debug mode")

class AsyncTestHandler(tornado.web.RequestHandler):

async def get(self):

operation = self.get_query_argument('operation')

if operation == 'with_two_waits':

await self._with_two_waits()

elif operation == 'with_gather':

await self._with_gather()

else:

self.finish('use operation=with_two_waits or operation=with_gather')

return

self.finish('finished ' + operation);

async def _with_two_waits(self):

print('_with_two_waits: start' + str(datetime.datetime.now()))

w1 = self._wait_loop("First loop", 8)

w2 = self._wait_loop("Second loop", 6)

await w1

await w2

print('_with_two_waits: finished' + str(datetime.datetime.now()))

async def _with_gather(self):

print('_with_gather: start' + str(datetime.datetime.now()))

coros = []

coros.append(self._wait_loop("First loop", 8))

coros.append(self._wait_loop("Second loop", 6))

await gather(*coros, return_exceptions=True)

print ('_with_gather: finished' + str(datetime.datetime.now()))

async def _wait_loop(self, loop_name, count):

for i in range(1, count + 1):

print(loop_name + ' ' + str(i) + '/' + str(count) + ' ' + str(datetime.datetime.now()))

await sleep(0.1)

print(loop_name + ' complete')

def start_web_app():

parse_command_line()

app = tornado.web.Application(

[

(r"/asnycTest", AsyncTestHandler),

],

debug=options.debug,

)

app.listen(options.port)

tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":

start_web_app()

2016-08-16

lorienn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值