Traceback (most recent call last):
File "filter_quotes.py", line 451, in <module>
stocks_filter.symbol_ids();
File "filter_quotes.py", line 216, in symbol_ids
self.symbol_ids_list = p.map(parallel_request, self.batch_result)
File "/usr/lib/python3.6/multiprocessing/pool.py", line 266, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.6/multiprocessing/pool.py", line 644, in get
raise self._value
File "/usr/lib/python3.6/multiprocessing/pool.py", line 424, in _handle_tasks
put(task)
File "/usr/lib/python3.6/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/usr/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
AttributeError: Can't pickle local object 'FilterStocks.symbol_ids.<locals>.parallel_request'
I am struggling with the following class method. I coded the function parallel_request()
in such a way I could use it with the Pool()
class. However, I got the above traceback and I don't know how to fix it. How could I use parallel_request()
function inside the class method symbol_ids
() so that it will fix that previous AttributeError?
from multiprocessing import Pool
def symbol_ids(self):
p = Pool()
def parallel_request(self, symbols):
response = requests.get(''.join((self.uri, symbols)), headers=self.headers)
return response.json()
self.symbol_ids_list = p.map(parallel_request, self.batch_result)
self.symbol_ids_result = [element['symbolId'] for element in self.symbol_ids_list]
UPDATE
The above function originated from the bottom test code.
I used the following class to request information from the Questrade API (http://www.questrade.com/api/documentation/rest-operations/market-calls/markets-quotes-id). I have over 11,000 stock symbols where I request the Questrade API with batches of 100 symbols.
from multiprocessing import Pool
import requests
import logging
import ast
import re
TOKEN_FILE = '../../token.txt'
class Test:
def __init__(self, access_token, api_server):
self.access_token = access_token
self.api_server = api_server
self.uri = '{}v1/symbols?names='.format(self.api_server)
self.headers = {'Authorization': 'Bearer {}'.format(self.access_token)}
def parallel_request(self, symbols):
print(symbols)
response = requests.get(''.join((self.uri, symbols)), headers=self.headers)
return response.json()
def process(self):
with open('Output.txt', 'r') as f:
batch_result = ast.literal_eval(f.read())
# symbol_ids_list = []
# for symbols in batch_result:
# response = requests.get(''.join((self.uri, symbols)), headers=headers)
# symbol_ids_list.extend(response.json().get('symbols'))
p = Pool()
test = p.map(self.parallel_request, batch_result)
if __name__ == '__main__':
with open(TOKEN_FILE, "r") as token_json:
token_json = ast.literal_eval(re.search('{.+}', token_json.read()).group(0))
access_token = token_json["access_token"]
api_server = token_json["api_server"].replace("\\", "")
obj = Test(access_token, api_server)
obj.process()