如何使在Python库异步 [英] How to make a library asynchronous in python

查看:133
本文介绍了如何使在Python库异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作,他们以小博大的龙卷风,但是他们没有异步库。是什么使库异步以便它可以是更适合于像龙卷风?是否有良好的例子还是我的猜测是没有财产以后你在做__进入__ __退出__ 这可以表明你不堵?

In my job they "leverage" tornado but they have no asynchronous libraries. What makes a library asynchronous so that it can be better suited to something like tornado? Are there any good examples or I guess is there somthing you do in __enter__ or __exit__ that can signal that you are not blocking?

我发现很难划伤一些材料一起。

I'm finding it difficult to scratch together some materials.

推荐答案

如果您的库不同步,不支持在龙卷风ioloop中运行,那么你可以做的唯一事情是在其他线程运行这些任务。

If your libraries are not asynchronous and do not support being run in the tornado ioloop, then the only thing you can do is run these tasks in other threads.

从本质上讲,有两种选择,取决于你是否要接收返回值的:

Essentially, there are two options, depending on whether you want to receive an return value or not:


  1. 您把您的工作队列中,一些线程拉从队列中工作,这些工作任务关==>没有返回值

  2. 或使用执行人库(蟒蛇3.2)和作品发送到遗嘱执行人,添加一个回调信号,即任务完成和龙卷风的ioloop交付控制权交还给(由另一个回调吊入循环)。

  1. you put your work in a queue and some threads pull work from the queue and work these tasks off ==> no return value
  2. or you use the executor library (python 3.2) and send the work into the executor, add a callback to signal that the task is finished and deliver control back to tornado's ioloop (by another callback hung into the loop).

如果 your_task_func 是您要卸载到另一个线程同步任务基本上是做到以下几点:

If your_task_func is the sync-task you want offload to another thread essentially do the following:

def callback(future):
    # here you want to do some stuff with the value future.result()

EXECUTOR.submit(
        your_task_func
        ).add_done_callback(
            lambda future: tornado.ioloop.IOLoop.instance().add_callback(
                partial(callback, future)))

关于这方面更多的细节可以在这个漂亮的写了

问候
马库斯

这篇关于如何使在Python库异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆