标签:oop result 效果 tool csharp while fun creat page
import asyncio import random import functools def page_done(url,task): print(‘下载结束:‘,url) print(‘task‘,task.result()) ##task.result() 获取回调内容 async def cron_scheduler(): page = 1 while True: url = ‘{}/{}‘.format(‘https://www.baidu.com‘,page) job = cron_job(url) task = asyncio.ensure_future(job) ##创建协程对象,并且加入到监控容器 #task = asyncio.create_task(job) 效果跟ensure_future其实是一样的,在高版本3.7采用 task.add_done_callback(functools.partial(page_done,url)) ##创建回调函数 await asyncio.sleep(0.3) page += 1 async def cron_job(url): n =random.randint(2,6) await asyncio.sleep(n) print(‘正在下载:‘,url) return url loop = asyncio.get_event_loop() loop.run_until_complete(cron_scheduler())
import asyncio
import random
import functools
def page_done(url,task):
print(‘下载结束:‘,url)
print(‘task‘,task.result()) ##task.result() 获取回调内容
async def cron_scheduler():
page = 1
while True:
url = ‘{}/{}‘.format(‘https://www.baidu.com‘,page)
job = cron_job(url)
task = asyncio.ensure_future(job) ##创建协程对象,并且加入到监控容器
#task = asyncio.create_task(job) 效果跟ensure_future其实是一样的,在高版本3.7采用
task.add_done_callback(functools.partial(page_done,url)) ##创建回调函数
await asyncio.sleep(0.3)
page += 1
async def cron_job(url):
n =random.randint(2,6)
await asyncio.sleep(n)
print(‘正在下载:‘,url)
return url
loop = asyncio.get_event_loop()
loop.run_until_complete(cron_scheduler())
标签:oop result 效果 tool csharp while fun creat page
原文地址:https://www.cnblogs.com/hero799/p/12610622.html