If I understand your situation, you want to run a management command fairly 
promptly after Django-server reboot, and not again for the duration of the 
server's uptime.

Cron is useful and convenient for repeated tasks.  And it can be made to 
handle once-and-done tasks, if you keep a bit of state somewhere.

There's a lot I don't know about the practical side of your situation, so I 
ask this question from ignorance. Can you use the __init__method of a 
special purpose app in INSTALLED_APPS?  I don't know if the INSTALLED_APPS 
are created during server startup, or only as needed in response to an 
incoming URL.

If each class (or package?) in INSTALLED_APPS gets an object created at 
server-boot time, you can do it within one of your apps.

__init__ can start a separate thread that delays for a specific time 
interval and then does your web-scraping scraping.  __init__() itself 
cannot do the wait, because there is an implicit expectation that 
__init__() finishes in a relatively short period of time.

If the trigger event for starting the web-scraping is something like 
first-visit-to-your-home-page, you could probably use django signals.  But 
I've never used signals and I can't tell you anything more about that.

  ---  Bill

On Friday, November 2, 2018 at 10:35:52 AM UTC-4, Bartosz Gańcza wrote:
>
> Hi everyone!
>
> I am somewhat of a Django beginner and I can't seem to find an easy 
> solution to what I need to do anywhere.
>
> I have a web scraping code I wish to run in the background automatically 
> (once) after the server is up and running. I use Docker to fire up the DB 
> and the web server itself but can't seem to be able to configure it to also 
> fire up the management command I configured that runs the scraping code (I 
> guess using Docker for that is either beyond my current knowledge or is 
> just not possible without using specialised tools like cron, which I don't 
> fully understand).
>
> Is there any way to do something like this in Django itself, without 
> resorting to "ready" function? (I did that at first but it stops the server 
> from running until the code completes and also runs it at least twice)
>
> Best,
> Bartosz
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/db6bf171-d920-4a16-af68-e52b0afffaa0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to