如何判断Python程序是否在工作而不必等待其完成?

时间:2018-11-02 00:44:01

标签: python pandas numpy xlwings

这是我的代码:

import xlwings as xw
import datetime as dt
import numpy as np
import pandas as pd
import threading
import time

#connect to workbook
wb = xw.Book(r'C:\Users\Ryan\AppData\Local\Programs\Python\Python37-32\constituents.xlsx')
sht = wb.sheets['constituents']

#store data in np array, pass to Pandas
a = sht.range('A2:C1760').options(np.array).value
df = pd.DataFrame(a)
df = df.rename(index=str, columns={0: "tickers", 1: "start_dates", 2: "end_dates"})

#initialize variables
start_quarter = 0
start_year = 0
fiscal_dates = []
s1 = pd.date_range(start='1/1/1964', end='12/31/2018', freq='B')
df2 = pd.DataFrame(data=np.ndarray(shape=(len(s1),500), dtype=str), index=s1)

#create list of fiscal quarters
def fiscal_quarters(start_year):
    year_count = start_year - 1
    quarter_count = 1
    for n in range(2019 - start_year):
        year_count += 1
        for i in range(1,5):
            fiscal_dates.append(str(quarter_count) + 'Q'+ str(year_count)[-2:])
            quarter_count += 1
        quarter_count = 1 

#iterate over list of tickers to create self-named spreadsheets
def populate_worksheets():
    for n in range(len(fiscal_dates)):
        wb.sheets.add(name=fiscal_dates[n])

#populate df2 with appropriate tickers
def populate_tickers():
    count = 0
    for n in range(len(s1)):
        for i in range(len(df['tickers'])):
            if df.loc[str(i), 'start_dates'] <= s1[n] and df.loc[str(i), 'end_dates'] > s1[n]:
                count += 1
                df2.loc[str(s1[n]), str(count)] = df.loc[str(i), 'tickers']
        count = 0

#run populate_tickers function with status updates
def pt_thread():
    t = threading.Thread(target=populate_tickers)
    c = 0
    t.start()
    while (t.is_alive()):
        time.sleep(5)
        count += 5
        print("Working... " + str(c) + 's')

首先,我在Python Shell中运行fiscal_quarters(1964),然后运行pt_thread(),这似乎特别耗费资源。这一点在我的笔记本电脑上已经运行了半个多小时。但是,无需等待它完成运行,是否有任何方法可以查看它是否按预期工作?还是全部?我认为它仍然在外壳上打印“正在工作...”,这是一个好兆头,但如果有问题,我想开始进行故障排除,而不是等待不确定的时间再放弃。

作为参考,s1系列包含〜17,500个项,而df ['tickers']列包含〜2,000个项,因此应该在35,000,000个迭代附近进行操作,每个迭代有4个操作。是很多吗,还是现代PC应该能够很快地完成此工作,而我的程序却可能无法正常工作?

1 个答案:

答案 0 :(得分:0)

如果您正在运行的循环花费很长时间,并且想了解发生了什么,则可以使用tqdm。它给出每秒迭代次数和估计的剩余时间。这是一个简单的示例:

from tqdm import tqdm

def sim(sims):
    x = 0
    pb = tqdm(total=sims, initial=x)
    while x < sims:
        x+=1
        pb.update(1)
    pb.close()
sim(5000000)