在Python中调用多处理时,TypeError:';tuple';对象不可调用
问题描述
我正在尝试使用多进程和队列执行以下脚本
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()
我得到的输出如下:
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
这里我看到我能够得到我想要的输出,但在那个之前有一个错误,这让我感到困惑。
有人能帮我理解一下这里的概念吗?
提前谢谢。
解决方案
target
应该是未调用的函数,您正在父进程中调用该函数,并尝试以调用结果作为目标启动Process
。更改:
p1 = multiprocessing.Process(target = Stock1.current_value(), args = (q,))
p2 = multiprocessing.Process(target = Stock1.fundems(), args = (q,))
收件人:
p1 = multiprocessing.Process(target=Stock1.current_value)
p2 = multiprocessing.Process(target=Stock1.fundems)
q
作为参数被删除,因为该对象是使用q
构造的,并且使用其自己的状态来访问它,而不是将其作为每个方法的参数接收。
相关文章