下面的代码有效,但是...
from concurrent.futures import ThreadPoolExecutor as PoolExecutor
date = "2020-06-12"
adjusted_symbol_list = ["AAPL", "MSFT", "NFLX"]
date_variable = get_specific_date_from_web()
def filter_2ndconfirm(symbol):
if date == "2020-06-12":
if make_calc() == True:
print(symbol)
def filter_2ndconfirm_workers(filter_2ndconfirm_workers, adjusted_symbol_list):
with PoolExecutor(max_workers=5) as executor:
for _ in executor.map(filter_2ndconfirm_workers, adjusted_symbol_list):
pass
filter_2ndconfirm_workers(filter_2ndconfirm, adjusted_symbol_list)
get_specific_date_from_web()正在计算,并在几个http连接之后获取日期,因此只能工作一次。上面的代码正常工作,但是如何插入另一个变量,即date_variable到filter_2ndconfirm func中呢?我可以像下面这样,但我不想为每个符号查询网络:
def filter_2ndconfirm(symbol):
date_variable = get_specific_date_from_web()
if date == date_variable:
print(symbol)
谢谢
如果您已经在filter_2ndconfirm函数外部定义了变量date_variable,则可以在函数内部访问其值,因此:
def filter_2ndconfirm(symbol):
if date == date_variable:
print(symbol)
否则,您可以将date_variable作为参数传递给函数:
将此新参数添加到filter_2ndconfirm
def filter_2ndconfirm(symbol, date_variable):
if date == date_variable:
print(symbol)
if date == "2020-06-12":
if make_calc() == True:
print(symbol)
然后将其他参数添加到executer.map
for _ in executor.map(filter_2ndconfirm_workers, adjusted_symbol_list, date_variable):
第二个选项通常更好,因为每个进程都使用自己的date_variable副本,但是您的选择可能取决于不同的因素,例如变量的大小以及是否在函数中进行编辑。] >