zoukankan      html  css  js  c++  java
  • 如何给ioloop.run_sync()中调用的函数传入参数

    问题
    如何给tornado.ioloop.IOLoop中的run_sync方法中调用的函数添加参数

    解决方案
    使用functools.partial

    解决示例

    from tornado import gen
    from tornado.ioloop import IOLoop

    @gen.coroutine
    def func():
        print('this is the %(name)s'%{'name': func.__name__})
        yield gen.sleep(6.0)
        print('%(num)d'%{'num': 10})


    @gen.coroutine
    def foo():
        print('this is the %(name)s'%{'name': foo.__name__})
        yield gen.sleep(1.5)
        print('%(num)d'%{'num': 5})


    @gen.coroutine
    def call():
        yield gen.sleep(0)
        print('this is the callback')


    @gen.coroutine
    def main(ioloop):
        ioloop.call_later(5, call)
        yield [func(), foo()]


    if __name__ == '__main__':
    from functools import partial
    ioloop = IOLoop.current()
    main = partial(main, ioloop=ioloop)
    ioloop.run_sync(main)

    总结
    利用functools.partial就可以把原本需要带参数调用的函数变成不需要带参数就可以调用,在python核心编程中称之为偏函数。
    ---------------------
    作者:Easy_to_python
    来源:CSDN
    原文:https://blog.csdn.net/hjhmpl123/article/details/53641121
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )
    BZOJ 2208: [Jsoi2010]连通数( DFS )
    js效果-鼠标提示框
    CSS-背景
    CSS-文本
    tomcat 安装 for Mac
    CSS选择器基础
    POJ3349-Snowflake Snow Snowflakes-Hash
    数据库的三大设计范式
    HDU5120-Intersection-两个圆环相交面积
  • 原文地址:https://www.cnblogs.com/b02330224/p/10213852.html
Copyright © 2011-2022 走看看