3

In a Flask based web application, taking two command line arguments ini filename, port number using argparse, in the same file celery app also defined.But while running the celery application I'm getting the above error.

import argparse
from flask import  Flask
from celery import Celery

app = Flask(__name__)
parser = argparse.ArgumentParser(prog="testpgm")
parser.add_argument('-c','--cfgfile', default='domain.ini', help="provide ini      file path")
parser.add_argument('-p','--port', default=5000, help="-p port number eg - 'python run.py -p <port>, default to 5000")
args = parser.parse_args()
ini_path = args.cfgfile
port = args.port

-------CELERY CONFIGS-------

app.config["CELERY_QUEUES"] = (
Queue('queue1', Exchange('queue1'), routing_key='queue1')
)
def make_celery(flaskapp):

   #getting celery broker uri
   celery_broker_uri=         CeleryBrokerWrapper().get_broker_uri(broker,username,password,host,port,vhost) 

   celeryinit = Celery(flaskapp.import_name, broker=celery_broker_uri)
   celeryinit.conf.update(flaskapp.config)
   taskbase = celeryinit.Task

   class ContextTask(taskbase):
      abstract = True

      def __call__(self, *args, **kwargs):
         with app.app_context():
             return taskbase.__call__(self, *args, **kwargs)

   celeryinit.Task = ContextTask
   return celeryinit

celery = make_celery(app)

but when I'm running celery using

celery -A testpgm.celery worker --loglevel=info --concurrency=5 -Q queue1

I'm getting the error like

testpgm: error: unrecognized arguments: -A testpgm.celery worker --loglevel=info --concurrency=5 -Q queue1

Its looks like an argparse error, how can I customise argparse for my application, with out having problem with celery's command line arguments..

user2264738
  • 302
  • 4
  • 18

2 Answers2

4

Had a similar issue, argparse also complained for me.

Quick Fix: use parse_known_args, as opposed to parse_args

args, unknown = parser.parse_known_args()

source: Python argparse ignore unrecognised arguments

Ugly Fix: define the celery worker args as part of the argparse your main app has

"Do it right" Fix: Consider using argparse in your main function so that celery does not clash with it

Handling argparse conflicts

Community
  • 1
  • 1
Maxwell Talbot
  • 155
  • 1
  • 8
1

you need to re-order the args:

celery worker -A testpgm.celery --loglevel=info --concurrency=5 -Q queue1
scytale
  • 12,346
  • 3
  • 32
  • 46
  • It did not help, I think celery argparse context is getting changed, – user2264738 Sep 11 '15 at 13:38
  • 2
    Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Sep 11 '15 at 14:21