Celery is not working on my Heroku - python

What on earth am I doing wrong?
I have recently found an awesome django template called django-skel. I started a project with it because it made it very easy to use heroku with django. It was all going great until I tried to get celery working. No matter what I tried I couldn't get my tasks to run. So I started a new bare bones app just to see if I could get it working without any of my other craziness preventing things.
This is my bare-bones app. I have this up and running on heroku. Django admin is working, I have my databases sync'd up and migrated. I am using CloudAMQP Little Lemur for my RabbitMQ. I see the requests queued up in the RabbitMQ interface, nothing happens. How I queue up the tasks is manually run in the shell:
from herokutest.apps.otgcelery.tasks import add
result = add.delay(2,2)
I make sure that I have all 3 dynos running, and still nothing.
Also I have it working locally.
I am sure there are tons of questions, and I'm willing to give them. Just please ask.

Thank you for everyones help.
There were a couple things that I ended up doing wrong. First thing was that I was importing the task incorrectly. All I had to do was:
from apps.otgcelery.tasks import add
result = add.delay(2,2)
Celery is very picky with how you import your tasks. The second issue is that CloudAMQP Free addon does not work out of the box with django-skel. They limit your number of connections to three, and how each thread kicks on it uses those connections up incredibly fast and your tasks just start not connecting. So I fixed this in a couple different ways. I tried another BigWigs RabbitMQ, and it worked great. However, because they were still in Beta I decided to try out Redis. That also worked great, and my tasks are firing off as fast as I can call them.
Once again thank you everyone for your help.

Related

How to do long running process in Django view?

I have a django web application and I have to create model for Machine Learning in a view.
It takes a long time so PythonAnyWhere does not allow it and it kills the process when it reaches 300 seconds. According to that, i want to ask two questions.
Without celery, django bg task or something else, my view which contains the long running process does not work in order. But when I use debugger, it works correctly. Probably, some lines of code try to work without waiting for each other without debugger. How can i fix this?
PythonAnyWhere does not support celery or other long running task packages. They suggest django-background-tasks but in its documentation, the usage is not explained clearly. So I could not integrate it. How can i integrate django-background-tasks?
Thank you.

Display progress of a long running Python task in Django

I currently have a typical Django structure set up for a project and one web application.
The web application is set up so that a user inputs some information, and this information is taken as the input to run a Python program.
This python program sometimes can take quite a while to finish (grabbing things from the web and doing some text mining scoring) - sometimes it can take multiple minutes to load.
On the command line, this program would periodically display where it was in the process (it'd first say how many things it found to score against, then it'd say where in the number of things found it is in the scoring process), which was very useful. However, when I moved this over to a Django set up, I no longer have this capability (at least, not in the same way since now this is sent to log files).
The way I set it up is that there is an input view, and then a results view. The results view takes the input and runs the Python program. It won't display the results until the entire program is run. So on the user side, the browser just sits there for sometimes minutes before the results are displayed. Obviously, this is not ideal.
Does anyone know of the best way to bring status information on a task to Django?
I've looked into Celery a little bit, but I think since I'm still a beginner in Django that I'm confusing myself with some of the documentation. For instance: even if the task is sent off asynchronously to a worker, how does the browser grab the current state of the program?? Also, consistent documentation seems to be lacking for celery on Django (I've seen people set up celery many different ways on their Django projects).
I would appreciate any input here, I've been stuck on this for a while now.
My first suggestion is to psychologically separate celery from django when you start to think of the two. They can run in the same environment, but celery is to asynchronous processes what django is to http requests.
Also remember that celery is unlike diango in that it requires other services to function; a message broker. So by using celery you will increase your architectural requirements.
To address you specific use case, you'll need a system to publish messages from each celery task to a message broker and your web client will need to subscribe to those messages.
There's a lot involved here, but the short version is that you can use Redis as your celery message broker as well as your pub/sub service to get messages back to the browser. You can then use e.g diango-redis-websockets to subscribe the browser to the task state messages in redis

Appropriate approach for Message Queue / Scheduled tasks in Django

I'm wondering what criteria would need to be considered when we need to use some kind of task queue in a django project, I'm thinking in performace, development speed, flexibility, etc.
I've been using Celery+RabbitMQ and Django-ztask+ZeroMQ indistinctly for a while (I'm sure there are another good ones), but I haven't an accurate canon for picking up the most suitable in each case.
Could you provide some peculiarities for each of them that allows the user chooses between them?, does it might include some another stable MQ approaches as well?
I can't provide much but I have used two different solutions, Celery+Redis and Celery+RabbitMQ.
I tried RabbitMQ first and after getting all its dependencies installed and spending some time wading through configs I got it working. It worked well, didn't drop anything, but I was always nervous about restarting (either it or the server) because I was never completely sure it would come back up. I'm sure that's my fault, but I couldn't work out what I'd done wrong.
So I thought I'd give Redis a try. Installed and configured it in about 3 minutes and it has worked without any of my attention since.
Now if only there was something easier to configure than Celery...

Run a Python script on Heroku on schedule (as a separate app)

OK so I'm working on an app that has 2 Heroku apps - one is the writer that writes to my DB after scraping a site, and one is the reader that consumes the said DB.
The former is just a Python script that has a kind of a while 1 loop - it's actually a Twitter stream. I want this to run every x minutes independent of what the reader is doing.
Now, running the script locally works fine, but I'm not sure how getting this to work on Heroku would work. I've tried looking it up, but could not find a solid answer. I read about background tasks, Redis queue, One-off dynos etc, but I'm not sure what to really use for my purpose. Some of my requirements are:
have the Python script keep logs of whatever I want.
in the future, I might want to add an admin panel for the writer, that will just show me stats of the script (and the logs). So hooking up this admin panel (flask) should be easy-ish and not break the script itself.
I would love any suggestions or pointers here.
I suggest writing the consumer as a server that waits around, then processes the stream on the timed interval. That is, you start it once and it runs forever, doing some processing every 10 minutes or so.
See: sched Python module, which handles scheduling events at certain times and running them.
Simpler: use Heroku's scheduler service.
This technique is simpler -- it's just straight-through code -- but can lead to problems if you have two of the same consumer running at the same time.

Server side python code runing continuosly per session

I have searched the forums for my question but im either searching for a thing naming it wrongly or the question is hard which i really doubt.
I am developing a web-app which would have an web-interface written in one of the MVC frameworks like django or even flask and allow user to login, will identify users session and allow to make some settings and also my app needs to run some python process(script which basically is a separate file) on the server on a per-session per-settings made by user basis. This process is quite long - can take even days to perform and shouldn't affect the execution and performance of MVC part of an app. Another issue is that this process should be run per user so the basic usage model of such app would be:
1. the user enters the site.
2. the user makes some settings which are mirrored to database.
3. the user pushes the launch button which executes some python script just for this user with the settings he has made.
4. the user is able to monitor some parameters of the script running based on some messages that the script itself generates.
I do understand that my question is related to the architecture of the app itself and i'm quite new to python and haven't had any experience of developing such complex application but I'm also quite eager to learn about it. I do understand the bricks from which my app should be built (like django or flask and the server-side script itself) but i know very little about how this elements should be glued together to create seamless environment. Please direct me to some articles related to this topic or recommend some similar threads or just give a clear high level explanation how such separate python processes could be triggered,run and monitored further on a per-user basis from controller part of MVC.
Celery is a great solution, but it can be overpowered for many setups. If you just need tasks to run periodically (once an hour, once a day, etc) then consider just using cron.
There's a lot less setup and it can get you quite far.
Celery is the perfect solution for you purposes.
Celery can easily run long tasks, but you have to write monitoring part. It's simple - you can use django-orm from a celery task.
Do not use django-celery or flask-celery applicattion - they are deprecated.

Categories