3

I am trying to execute the following script by using multiprocessing and queues,

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 am getting the output as the following:

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 am able to get the output which I wanted, but there was an error before that which is kind a making me confused.

Can anyone please help me understand the concept here.

Thanks in advance.

1 Answer 1

3

The target should be an uncalled function, you're calling the function in the parent process and trying to launch a Process with the results of the call as the target. Change:

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 an argument because the object was constructed with q, and uses its own state to access it, it doesn't receive it as an argument to each method.

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

8 Comments

Nope.. it did not work. It threw me the following error,
Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap Process Process-1: Traceback (most recent call last): self.run() File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run self._target(*self._args, **self._kwargs) TypeError: fundems() takes exactly 1 argument (2 given) self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run self._target(*self._args, **self._kwargs) TypeError: current_value() takes exactly 1 argument (2 given)
p1 = multiprocessing.Process(target = Stock1.current_value) p2 = multiprocessing.Process(target = Stock1.fundems)
This gave me the answer
|

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.