0

When I run this script I want to add an argument in the console for the numbers of the consumers that I want to run together. For example adsconsumer.py nb=10 would mean that I will run this script 10 times simultaneously.

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('nb=')
    nb = parser.parse_args()

    for i in range(nb):
        thread = Thread(target=process_consumer(), args=())
        thread.start()
        thread.join()

I get this error:

TypeError: 'Namespace' object cannot be interpreted as an integer

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
joes
  • 137
  • 1
  • 1
  • 7
  • Can you please add the traceback? When do not know what `Namespace` is. – Guimoute Mar 27 '19 at 09:33
  • guess `nb` isn't an integer, but a string... Try forcing the `nb` to be positive integer, check this out: https://stackoverflow.com/questions/14117415/in-python-using-argparse-allow-only-positive-integers – Aaron_ab Mar 27 '19 at 09:37
  • i didn't understand you can you explain more for me because i'm a beginner – joes Mar 27 '19 at 09:39
  • @Aaron_ab can you help me with code please – joes Mar 27 '19 at 10:35

1 Answers1

1

You are using nb as a numerical value so it should be. Try:

print(type(nb))

It should return str as the error indicates.

just convert it to integer as follows:

or i in range(int(nb)):

Or tell the agparser to treat the input as an integer:

def main():
parser = argparse.ArgumentParser()
parser.add_argument('nb=',type=int)
nb = parser.parse_args()
Ahmed Hawary
  • 461
  • 4
  • 15
  • when i try type=int in the agparser i get this error – joes Mar 27 '19 at 10:00
  • usage: selogerAdsconsumer.py [-h] nb= selogerAdsconsumer.py: error: argument nb=: invalid int value: 'nb=10' – joes Mar 27 '19 at 10:00
  • Is there a need for 'nb=' in your string? I believe you should remove it. does ab a variable that holds the integer value that you want to pass? – Ahmed Hawary Mar 27 '19 at 10:05
  • yeah i want to write in the console when i run the script the file name and the number of consumer like "selogerAdsconsumer.py nb=10" – joes Mar 27 '19 at 10:08
  • SO the problem ould be in "range(nb)" as np will be holding a string and you are trying to interpret it as an integer. – Ahmed Hawary Mar 27 '19 at 10:10
  • What is the variable which holds the number of consumer? – Ahmed Hawary Mar 27 '19 at 10:11
  • nb holds the number of consumer – joes Mar 27 '19 at 10:12
  • According to your code nb which is defined as "nb = parser.parse_args() " this is a string, not an integer. There should be another var holds the number of consumers which you can add to the string 'nb=' using string formatting and use in the range as a numerical value without any errors. – Ahmed Hawary Mar 27 '19 at 10:21
  • okay , please can you help me with the code because i'm a beginner – joes Mar 27 '19 at 10:23