首页 > 其他分享 >关于asyncio.create_task异步并发执行的研究

关于asyncio.create_task异步并发执行的研究

时间:2023-11-10 19:44:22浏览次数:29  
标签:10 task create datetime 2023 print TaskHandler now asyncio

关于asyncio.create_task异步并发执行的研究

# 不在乎结果版本

async def do_some_thing(a, b):
    time.sleep(3)
    print(f"{datetime.datetime.now()} handle do_some_thing with a:{a} and b:{b}")
    return a + b


class TaskHandler(tornado.web.RequestHandler):
    async def get(self):
        print(f'{datetime.datetime.now()} start TaskHandler')
        res = asyncio.create_task(do_some_thing(1, 2))
        print(f'{datetime.datetime.now()} end TaskHandler')
        self.write("success")
        print(f"write success time: {datetime.datetime.now()}")


def make_app():
    return tornado.web.Application([
        (r"/task", TaskHandler),
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
    

# 2023-11-10 19:34:41.035492 start TaskHandler
# 2023-11-10 19:34:41.035519 end TaskHandler
# write success time: 2023-11-10 19:34:41.035529
# 2023-11-10 19:34:44.040885 handle do_some_thing with a:1 and b:2
# 等待结果

async def do_some_thing(a, b):
    time.sleep(3)
    print(f"{datetime.datetime.now()} handle do_some_thing with a:{a} and b:{b}")
    return a + b


class TaskHandler(tornado.web.RequestHandler):
    async def get(self):
        print(f'{datetime.datetime.now()} start TaskHandler')
        res = asyncio.create_task(do_some_thing(1, 2))
        print(f'{datetime.datetime.now()} end TaskHandler')
        await res
        print(f"{datetime.datetime.now()} res.result(): {res.result()}")
        self.write("success")
        print(f"write success time: {datetime.datetime.now()}")


def make_app():
    return tornado.web.Application([
        (r"/task", TaskHandler),
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
    
# 2023-11-10 19:36:40.486563 start TaskHandler
# 2023-11-10 19:36:40.486649 end TaskHandler
# 2023-11-10 19:36:43.491812 handle do_some_thing with a:1 and b:2
# 2023-11-10 19:36:43.492300 res.result(): 3
# write success time: 2023-11-10 19:36:43.492461

标签:10,task,create,datetime,2023,print,TaskHandler,now,asyncio
From: https://www.cnblogs.com/pywjh/p/17824894.html

相关文章

  • EF报错:Unable to create an object of type 'XXXXXXX'. For the different patterns s
    这个是在EF迁移的时候报错: 解决方案:修改你的MyDbcontext:  代码如下:publicclassStoreDbContexttFactory:IDesignTimeDbContextFactory<‘你的类名’>{public‘你的类名’CreateDbContext(string[]args){varoptionsBuilder=newDbContextOptionsBuilder<‘......
  • Java中的Runnable、Callable、Future、FutureTask的区别与示例
    Java中存在Runnable、Callable、Future、FutureTask这几个与线程相关的类或者接口,在Java中也是比较重要的几个概念,我们通过下面的简单示例来了解一下它们的作用于区别。Runnable其中Runnable应该是我们最熟悉的接口,它只有一个run()函数,用于将耗时操作写在其中,该函数没有返回值。然......
  • LINUX:Error while compiling statement: FAILED: RuntimeException Cannot create sta
    问题截图 可以看到是user=root,权限不够导致 观察发现用的是root用户更改为hadoop用户,也即是可以启动hive的用户 插入成功。 ......
  • 无涯教程-批处理 - TASKKILL函数
    此批处理命令结束一个或多个任务。TASKKILL-语法Taskkill/im[taskname]TASKKILL-示例@echooffTaskkill/immspaint.exe上面的命令会将终止消息发送到MSPaint的任何打开的程序。参考链接https://www.learnfk.com/batch-script/batch-script-taskkill.html......
  • CF1359D Yet Another Yet Another Task
    貌似没有线段树做法。记\(s\)为\(a\)的前缀和数组。对于一个确定的右端点\(r\)和左端点\(l\),它对于答案的贡献是\(s_r-s_{l-1}-max\{a_i\},l\lei\ler\),如果枚举右端点,令\(c_l=s_{l-1}+max\{a_i\},l\lei\)。那么其实就是要求\(1\lek\ler-1\)的\(min\{c_k\}\)。线......
  • MySQL中表复制:create table like 与 create table as select
    1   CREATETABLEALIKEB此种方式在将表B复制到A时候会将表B完整的字段结构和索引复制到表A中来。2.  CREATETABLEAASSELECT*FROMB此种方式只会将表B的字段结构复制到表A中来,但不会复制表B中的索引到表A中来。这种方式比较灵活可以在复制原表表结构的同时指定要复......
  • Oracle创建表语句(Create table)语法详解及示例
     创建表(Createtable)语法详解1. ORACLE常用的字段类型ORACLE常用的字段类型有VARCHAR2(size)可变长度的字符串,必须规定长度CHAR(size)固定长度的字符串,不规定长度默认值为1NUMBER(p,s)数字型p是位数总长度,s是小数的长度,可存负数最长38位.不够位时会四舍五入.......
  • 在Vue 3中如何在created钩子中进行API调用?
    在Vue3中,您可以使用setup函数来替代Vue2中的created生命周期钩子,并在其中进行API调用。下面是一个示例:import{ref,onMounted}from'vue'importaxiosfrom'axios'exportdefault{setup(){//创建一个响应式变量来存储API的返回数据constdata=ref(null......
  • Task基本用法
     什么是异步同步和异步主要用于修饰方法。当一个方法被调用时,调用者需要等待该方法执行完毕并返回才能继续执行,这个为异步方法。当一个方法被调用时立即返回,并获取一个线程执行该方法内部的业务,调用者不用等待该方法执行完毕,我们称为这个方法为异步方法。 《IllustratedC#7F......
  • Task异步多线程
    不废话,直接贴上代码...【1】直接实现多线程:`usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceTask异步多线程{classProgram{staticvoidMain(string[]args){......