[FIXED] Python multiprocessing library thinks multilettered args are multiple args

Issue

Ive been trying to program this piece of code that analsizes stocks and whenenver i try to pass a stock symbol such as "GOOG" trought multiproccess it takes each letter of "GOOG" as a different argument.

import multiprocessing as mp

def main():
    for value in stocks:
        
        p = mp.Process(target=nnfuncs.compare, args=value["symbol"])
        
        p.start()
        nnstruct.processes.append(p) 
        
    for process in nnstruct.processes:
        process.join()

if __name__ == '__main__':
    main()

The "stocks" var contains "AWP" "ACP" "JEQ" "ASGI" "AOD".

If i were to run that it would tell me that "TypeError: compare() takes 1 positional argument but 4 were give" even though those are all one arguments, it multiproccesing takes it as each letter of the symbol is one different argument.


Solution

The args parameter in multiprocessing.Process are interpreted as a tuple (see the documentation), so your text arguments are being interpreted as tuple("AWP") == ("A", "W", "P"). You probably want to pass your argument as args=(value["symbol"],).



Answered By - M.O.
Answer Checked By - David Goodson (Python.Easybugfix Volunteer)

Related Posts

Post a Comment