开发者

Django, Python calling Python code without waiting for response?

开发者 https://www.devze.com 2023-01-17 15:20 出处:网络
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 f

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 wi开发者_开发技巧th just calling other python code?

Thanks for any advice.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号