multiprocessing initialising a function in a class

2019-08-13 06:30发布

问题:

I am trying to initialise a function in a class using multiprocessing, by calling it from a function, which is inside the same same class

    def Streaminit(self,_track):
        self.twitterStream = tweepy.Stream(self.auth, Twitterapi.Listener())
        self.twitterStream.filter(track=_track)

    def Stream(self,track=""):
        self.streamobj = multiprocessing.Process(target = self.Streaminit(),args=(track,))

but when I call stream it raises an error

TypeError: Streaminit() takes exactly 2 arguments (1 given)

What am I doing wrong in this

回答1:

self.streamobj = multiprocessing.Process(target = self.Streaminit(),args=(track,))

You're calling the Streaminit function here with no arguments, and it takes one argument (plus self). So naturally it'll cause an error.

What it looks like you wanted to do was pass the function itself to multiprocessing.Process:

self.streamobj = multiprocessing.Process(target=self.Streaminit, args=(track,))