如何同时运行这两个python例程?
第一个例程
for i in tqdm_notebook(range(0,len(stock_ticker.Symbol))):
try:
ticker_df=pd.DataFrame(get_data(np.array(stock_ticker.Symbol)[i]))
ticker_df.to_csv("E:\\Stock Database\\Stock Prices\\%s.txt"%np.array(stock_ticker.Symbol)[i])
并同时运行此
第二次例行
for i in tqdm_notebook(range(0,len(stock_ticker.Symbol))):
try:
ticker_df=pd.DataFrame(get_income_statement(np.array(stock_ticker.Symbol)[i]))
ticker_df.to_csv("E:\\Stock Database\\Income Statement\\%s.txt"%np.array(stock_ticker.Symbol)[i])
答案 0 :(得分:0)
最简单的方法是使用标准库中随附的threading
软件包。将两个模块包装在函数中,然后将它们包装在线程中应该可以完成工作-即
def write_prices():
for i in tqdm_notebook(range(0,len(stock_ticker.Symbol))):
try:
ticker_df=pd.DataFrame(get_data(np.array(stock_ticker.Symbol)[i]))
ticker_df.to_csv("E:\\Stock Database\\Stock Prices\\%s.txt"%np.array(stock_ticker.Symbol)[i])
def write_incomes():
for i in tqdm_notebook(range(0,len(stock_ticker.Symbol))):
try:
ticker_df=pd.DataFrame(get_income_statement(np.array(stock_ticker.Symbol)[i]))
ticker_df.to_csv("E:\\Stock Database\\Income Statement\\%s.txt"%np.array(stock_ticker.Symbol)[i])
import threading
# construct the threads
t1 = threading.Thread(target=write_prices)
t2 = threading.Thread(target=write_incomes)
# start the threads
[t.start() for t in (t1, t2)]
# wait until they finish
[t.join() for t in (t1, t2)]
这应该起作用,但是如果您在get_data(...)
和get_income_statement(...)
之间共享值,则可能会遇到需要使用锁解决的问题。