Django、Python调用Python代码而不等待响应?

发布于 2024-09-24 19:27:00 字数 388 浏览 2 评论 0原文

我正在使用 Django,并且正在制作一些长时间运行的进程,我只是通过我的 Web 用户界面与之交互。例如,它们会一直运行,每隔几分钟检查一次数据库值,并且仅在数据库值发生变化时停止(为布尔值 true false)。所以,我希望能够使用 Django 与这些进行交互,但是我不确定如何做到这一点。当我使用 PHP 时,我有一些方法可以做到这一点,我认为用 Python 来做会更容易,但我在搜索中找不到任何相关内容。

基本上,我想要做的就是执行 python 代码而不等待它完成,因此它只是开始执行,然后继续执行 django 所需的任何其他操作,快速向用户返回一个新页面。

我知道有多种方法可以调用外部程序,所以我想这可能是唯一的方法?有没有办法只调用其他 python 代码来做到这一点?

感谢您的任何建议。

I am using Django and am making some long running processes that I am just interacting with through my web user interface. Such as, they would be running all the time, checking a database value every few minutes and stopping only if this has changed (would be boolean true false). So, I want to be able to use Django to interact with these, however am unsure of the way to do this. When I used to use PHP I had some method of doing this, figure it would be even easier to do in Python but am not able to find anything on this with my searches.

Basically, all I want to be able to do is to execute python code without waiting for it to finish, so it just begins execute then goes on to do whatever else it needs for django, quickly returning a new page to the user.

I know that there are ways to call an external program, so I suppose that may be the only way to go? Is there a way to do this with just calling other python code?

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-10-01 19:27:00

不能保证它,因为我还没有使用过它,但“Celery”几乎可以满足您的要求,并且最初是专门为 Django 构建的。

http://celeryproject.org/

他们的示例显示了一个添加两个数字的简单任务:

from celery.decorators import task

@task
def add(x, y):
    return x + y

您可以在后台执行该任务,或者等待它完成:

>>> result = add.delay(8, 8)
>>> result.wait() # wait for and return the result
16

您可能需要安装 RabbitMQ 还要让它工作,所以它可能比您正在寻找的解决方案更复杂,但它会实现您的目标。

Can't vouch for it because I haven't used it yet, but "Celery" does pretty much what you're asking for and was originally built specifically for Django.

http://celeryproject.org/

Their example showing a simple task adding two numbers:

from celery.decorators import task

@task
def add(x, y):
    return x + y

You can execute the task in the background, or wait for it to finish:

>>> result = add.delay(8, 8)
>>> result.wait() # wait for and return the result
16

You'll probably need to install RabbitMQ also to get it working, so it might be more complicated of a solution than you're looking for, but it will achieve your goals.

_失温 2024-10-01 19:27:00

您需要一个异步消息管理器。我有关于将 Gearman 与 Django 集成的教程。任何可pickle的Python对象都可以发送到Gearman,Gearman将完成所有工作并将结果发布到您想要的任何地方;该教程包括回发到 Django 数据库的示例(还展示了如何在 Django 之外使用 ORM)。

You want an asynchronous message manager. I've got a tutorial on integrating Gearman with Django. Any pickleable Python object can be sent to Gearman, which will do all the work and post the results wherever you want; the tutorial includes examples of posting back to the Django database (it also shows how to use the ORM outside of Django).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文