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

3
  • Sorry it was typo, now i've modified it to queue1 Commented Sep 11, 2015 at 13:33
  • celery -h is working fine for me.. Commented Sep 11, 2015 at 13:38
  • I would try 2 things - displaying sys.argv. That's what your parser is reading. We need to know what's in it sees. And use parse_known_args (see docs) so the parser doesn't choke on stuff it does not recognize. Commented Sep 11, 2015 at 16:07

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

you need to re-order the args:

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

2 Comments

It did not help, I think celery argparse context is getting changed,
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.