本文介绍了`如预期eventlet.spawn`不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的数据分析任务的Web UI。

I'm writing a web UI for data analysis tasks.

下面是它应该工作的方式:

Here's the way it's supposed to work:

用户后指定像学习率,我创建了一个新的参数任务记录,然后完成这个任务,执行程序启动asyncly(遗嘱执行人可能需要较长的时间来运行),用户会被重定向到其他网页。

After a user specifies parameters like dataset and learning rate, I create a new task record, then a executor for this task is started asyncly (The executor may take a long time to run.), and the user is redirected to some other page.

寻找一个后异步库蟒蛇,我开始与 eventlet ,这里就是我在写的视图功能:

After searching for an async library for python, I started with eventlet, here's what I wrote in a flask view function:

db.save(task)
eventlet.spawn(executor, task)
return redirect("/show_tasks")

通过上面的code,执行人没有执行的。

With the code above, the executor didn't execute at all.

什么可能是我的code的问题呢?或者,也许我应该试试别的?

What may be the problem of my code? Or maybe I should try something else?

推荐答案

您需要的的一些系统库,以便使eventlet工作。下面是一个最小的工作示例(也可以作为):

You'll need to patch some system libraries in order to make eventlet work. Here is a minimal working example (also as gist):

#!/usr/bin/env python

from flask import Flask
import time
import eventlet

eventlet.monkey_patch()

app = Flask(__name__)
app.debug = True

def background():
    """ do something in the background """
    print('[background] working in the background...')
    time.sleep(2)
    print('[background] done.')
    return 42

def callback(gt, *args, **kwargs):
    """ this function is called when results are available """
    result = gt.wait()
    print("[cb] %s" % result)

@app.route('/')
def index():
    greenth = eventlet.spawn(background)
    greenth.link(callback)
    return "Hello World"

if __name__ == '__main__':
    app.run()

更多关于是:



  • http://eventlet.net/doc/patching.html#monkey-patch

一个写像Eventlet库的挑战之一是内置的网络库本身不支持的那种合作产生了我们所需要的。

这篇关于`如预期eventlet.spawn`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:29