暂停 for 循环,保存进度并再次恢复?
Pause a for loop, save progress and resume it another time?
在 python(3) 中,我正在编写一个使用 递归 for 循环 的脚本(for 循环多次调用它自己的函数)而且我知道脚本只会在大约 45 小时后完成,所以我正在寻找一种方法将进度保存在文件中,关闭 python,关闭然后再恢复它天.
有没有办法保存进度然后在另一个(新)会话中恢复?
递归循环示例:
i=1
def for_loop(i):
if i==10:
print(i)
#code
else:
for x in range(0, 10):
#code
for_loop(i+1)
完整代码:
def TheLoop(num_N, num_a, num_A, num_S, i):
global Key
global num_Keys
global num_Declined
global KeyTogether
global f
if i > 8:
if (num_N > 0) and (num_a > 0) and (num_A > 0) and (num_S > 0):
num_Keys = num_Keys + 1
KeyTogether = ''
for t in range(8):
try:
number = int(Key[t])
Key[t] = str(Key[t])
except ValueError:
pass
KeyTogether += Key[t]
#Output Key to a file
f.write(KeyTogether + "\n")
else:
# Invalid Key
num_Declined = num_Declined + 1
else:
if (num_N < 4) and ((i < 8) or ((num_a > 0) and (num_A > 0) and (num_S > 0))):
num_N = num_N + 1
for x in range(0, 10):
Key[i-1] = x
TheLoop(num_N, num_a, num_A, num_S, i+1)
if (num_a < 4) and ((i < 8) or ((num_N > 0) and (num_A > 0) and (num_S > 0))):
num_a = num_a +1
for c in "abcdefghijklmnopqrstuvwxyz":
Key[i-1] = c
TheLoop(num_N, num_a, num_A, num_S, i+1)
if (num_A < 4) and ((i < 8) or ((num_N > 0) and (num_a > 0) and (num_S > 0))):
num_A = num_A + 1
for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
Key[i-1] = c
TheLoop(num_N, num_a, num_A, num_S, i+1)
if (num_S < 4) and ((i < 8) or ((num_N > 0) and (num_a > 0) and (num_A > 0))):
num_S = num_S + 1
for c in '"'"!#$%&'()*+,-./:;<=>?@[\]^_`{|}~":
Key[i-1] = c
TheLoop(num_N, num_a, num_A, num_S, i+1)
Start = time.time()
TheLoop(0,0,0,0,1)
duration = time.time() - Start
print("Duration: " + str(duration))
print("Declined Keys: ", num_Declined)
print("Valid Keys: ", num_Keys)
duracionstr = str(duration)
f.write("Duration: " + durationstr)
我在项目中的一些长 运行 循环中遇到了同样的问题。
我创建了一个运行循环并具有一些额外优点的函数。
- 每 N 次迭代保存进度(pickle)
- 从上次中断的地方继续
- 如果输入包含多个可迭代对象,我将遍历它们的排列。
- 视觉效果不错的进度条。
如有任何问题,请告诉我,我很乐意提供帮助。
该函数在 pip 上可用:
pip install daves_utilities
用法:
from daves_utilities.for_long import for_long
letters = ["a","b","c","d"]
numbers = [10,20,40,100,500,1000]
attribute_dict = {"letters": letters, "numbers": numbers, "input_bool":True}
df_out = for_long(iter_fun = my_function, iter_attr = attribute_dict,\
save_every_n = 10, path = "./")
在 python(3) 中,我正在编写一个使用 递归 for 循环 的脚本(for 循环多次调用它自己的函数)而且我知道脚本只会在大约 45 小时后完成,所以我正在寻找一种方法将进度保存在文件中,关闭 python,关闭然后再恢复它天.
有没有办法保存进度然后在另一个(新)会话中恢复?
递归循环示例:
i=1
def for_loop(i):
if i==10:
print(i)
#code
else:
for x in range(0, 10):
#code
for_loop(i+1)
完整代码:
def TheLoop(num_N, num_a, num_A, num_S, i):
global Key
global num_Keys
global num_Declined
global KeyTogether
global f
if i > 8:
if (num_N > 0) and (num_a > 0) and (num_A > 0) and (num_S > 0):
num_Keys = num_Keys + 1
KeyTogether = ''
for t in range(8):
try:
number = int(Key[t])
Key[t] = str(Key[t])
except ValueError:
pass
KeyTogether += Key[t]
#Output Key to a file
f.write(KeyTogether + "\n")
else:
# Invalid Key
num_Declined = num_Declined + 1
else:
if (num_N < 4) and ((i < 8) or ((num_a > 0) and (num_A > 0) and (num_S > 0))):
num_N = num_N + 1
for x in range(0, 10):
Key[i-1] = x
TheLoop(num_N, num_a, num_A, num_S, i+1)
if (num_a < 4) and ((i < 8) or ((num_N > 0) and (num_A > 0) and (num_S > 0))):
num_a = num_a +1
for c in "abcdefghijklmnopqrstuvwxyz":
Key[i-1] = c
TheLoop(num_N, num_a, num_A, num_S, i+1)
if (num_A < 4) and ((i < 8) or ((num_N > 0) and (num_a > 0) and (num_S > 0))):
num_A = num_A + 1
for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
Key[i-1] = c
TheLoop(num_N, num_a, num_A, num_S, i+1)
if (num_S < 4) and ((i < 8) or ((num_N > 0) and (num_a > 0) and (num_A > 0))):
num_S = num_S + 1
for c in '"'"!#$%&'()*+,-./:;<=>?@[\]^_`{|}~":
Key[i-1] = c
TheLoop(num_N, num_a, num_A, num_S, i+1)
Start = time.time()
TheLoop(0,0,0,0,1)
duration = time.time() - Start
print("Duration: " + str(duration))
print("Declined Keys: ", num_Declined)
print("Valid Keys: ", num_Keys)
duracionstr = str(duration)
f.write("Duration: " + durationstr)
我在项目中的一些长 运行 循环中遇到了同样的问题。 我创建了一个运行循环并具有一些额外优点的函数。
- 每 N 次迭代保存进度(pickle)
- 从上次中断的地方继续
- 如果输入包含多个可迭代对象,我将遍历它们的排列。
- 视觉效果不错的进度条。
如有任何问题,请告诉我,我很乐意提供帮助。
该函数在 pip 上可用:
pip install daves_utilities
用法:
from daves_utilities.for_long import for_long
letters = ["a","b","c","d"]
numbers = [10,20,40,100,500,1000]
attribute_dict = {"letters": letters, "numbers": numbers, "input_bool":True}
df_out = for_long(iter_fun = my_function, iter_attr = attribute_dict,\
save_every_n = 10, path = "./")