Python – Type error: ‘tuple’ object is not callable while calling multiprocessing in python

Type error: ‘tuple’ object is not callable while calling multiprocessing in python… here is a solution to the problem.

Type error: ‘tuple’ object is not callable while calling multiprocessing in python

I’m trying to use multiprocessing and queuing to execute the following script

from googlefinance import getQuotes
from yahoo_finance import Share
import multiprocessing

class Stock:

def __init__(self,symbol,q):
        self.symbol = symbol
        self.q = q

def current_value(self):
        current_price =self.q.put(float(getQuotes(self.symbol)[0]['LastTradeWithCurrency']))
        return current_price

def fundems(self):

marketcap = self.q.put(Share(self.symbol).get_market_cap())
        bookvalue = self.q.put(Share(self.symbol).get_book_value())
        dividend = self.q.put(Share(self.symbol).get_dividend_share())
        dividend_paydate = self.q.put(Share(self.symbol).get_dividend_pay_date())
        dividend_yield = self.q.put(Share(self.symbol).get_dividend_yield())

return marketcap, bookvalue, dividend, dividend_paydate, dividend_yield

def main():
    q = multiprocessing. Queue()
    Stock1 = Stock('aapl', q) 

p1 = multiprocessing. Process(target = Stock1.current_value(), args = (q,))
    p2 = multiprocessing. Process(target = Stock1.fundems(), args = (q,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

while q.empty() is False:
        print q.get()

if __name__ == '__main__':
    main()

I get the following output:

Process Process-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
TypeError: 'tuple' object is not callable
139.52
732.00B
25.19
2.28
2/16/2017
1.63

Here I see that I was able to get the output I wanted, but there was a bug that confused me before.

Can anyone help me understand the concept here.

Thanks in advance.

Solution

target

should be an uncalled function that you call in the parent process and try to start a Process with the result of the call as the target

p1 = multiprocessing. Process(target = Stock1.current_value(), args = (q,))
p2 = multiprocessing. Process(target = Stock1.fundems(), args = (q,))

to:

p1 = multiprocessing. Process(target=Stock1.current_value)
p2 = multiprocessing. Process(target=Stock1.fundems)

q

is removed as a parameter because the object is constructed with q and accessed with its own state, it does not receive it as a parameter for every method.

Related Problems and Solutions