This is my code without concurrent.futures:
import finviz
def finvizxl(ticker):
data = finviz.get_stock(ticker)
globals()[ticker] = data
finvizxl('AAPL')
print(AAPL)
print(AAPL)
returns stock information on AAPL using the finviz module and the finviz.get_stock method. The globals() function converted my 'AAPL' string to a variable to store data
This is my code with concurrent.futures:
import finviz
import concurrent.futures
def finvizxl(ticker):
data = finviz.get_stock(ticker)
globals()[ticker] = data
if __name__ == '__main__':
with concurrent.futures.ProcessPoolExecutor() as executor:
x = ['AAPL', 'TSLA', 'GME']
executor.map(finvizxl, x)
print(AAPL)
My Goal is to run my function finvizxl()
concurrently and store data
in the converted strings 'AAPL', 'TSLA', 'GME'
respectively, that way when i type print(aapl)
, the stock information for the respective ticker will be returned. However, i am getting this error:
NameError: name 'AAPL' is not defined
When i put print(AAPL)
in the function instead,
def finvizxl(ticker):
data = finviz.get_stock(ticker)
locals()[ticker] = list(data.values())
print(AAPL)
if __name__ == '__main__':
with concurrent.futures.ProcessPoolExecutor() as executor:
x = ['AAPL', 'TSLA', 'GME']
executor.map(finvizxl, x)
The program runs but nothing is returned.
However, when i do this:
def finvizxl(ticker):
data = finviz.get_stock(ticker)
locals()[ticker] = list(data.values())
print(locals()[ticker])
if __name__ == '__main__':
with concurrent.futures.ProcessPoolExecutor() as executor:
x = ['AAPL', 'TSLA', 'GME']
executor.map(finvizxl, x)
All 3 of my tickers ['AAPL', 'TSLA', 'GME']
have their information returned in the terminal.
I am probably missing something basic as i just started learning python. Thanks for your help.
Not if you're using the process pool, no. Each pool item has its own process, with a completely separate Python interpreter and a completely separate memory space. concurrent
tries to shuffle over the things you need, but there is no live connection.
If you need to ship results back, you might consider using a multiprocessing.Queue
. That knows how to marshal things across the process boundary.