0

I have a Celery task that I run locally using celery -A tasks worker --loglevel=info or sometimes it runs on a live server.

@app.task
def example_task_calling_api():
      url = "http://127.0.0.1:8000/" 
      #  url = "http://www.example.com/api/"
      content = requests.get(url)
      #Do something with content etc

At the moment I uncomment the url depending on what environment I'm running.

My question is, how can I pass into celery settings or extra args that I can use to setup the correct URL?

PS... I DON'T mean passing args to a task i.e. Task(123) but somesort of system wide variable set upon starting celery.

i.e. celery -A tasks worker --loglevel=info -LOCALDEV
Prometheus
  • 32,405
  • 54
  • 166
  • 302

1 Answers1

1

One way would be to set the url as an environmental variable, and use python os.environ to see if it is set and or set a default.

If you have multiple variables you'd like to config based on your env's: ie. demo, develop, production ect, you could use a python file and store all your varialbes in it. Then when your application starts up you could select the correct settings file based on a command line argument or from an env variable.

The above suggestion is how django framework handles your issue.

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145