有没有办法过早地停止泡菜过程?
Is there any way to stop a pickle process prematurely?
我有一个程序,我使用 pickle
来保存和加载相当大和复杂的数据集,这可能需要长达 30 秒。该过程在辅助线程中处理,并带有一个显示进度的对话框和一个取消它的按钮。现在真的有办法停止倾销或储蓄吗?我的感觉是进程一启动,不管怎样都会结束。
所以基本上,如果你有类似
的东西
import pickle
data = 1
with open('./file', 'wb') as file:
pickle.dump(data, file, pickle.HIGHEST_PROTOCOL)
# or
data = pickle.load(file)
有什么方法可以阻止文件被(覆盖)写入吗?还是正在加载到内存中的数据?
一种方法是将您的数据集分成几块,并在每一步检查是否取消,例如:
import pickle
cancel = False
dataset_path = './dataset.pkl'
dataset = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
with open(dataset_path, 'wb') as handle:
for data in dataset:
if cancel:
break
pickle.dump(data, handle, pickle.HIGHEST_PROTOCOL)
# or
dataset = []
with open(dataset_path, 'rb') as handle:
while 1:
try:
dataset.append(pickle.load(handle))
except EOFError:
break
print(dataset)
输出:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
我有一个程序,我使用 pickle
来保存和加载相当大和复杂的数据集,这可能需要长达 30 秒。该过程在辅助线程中处理,并带有一个显示进度的对话框和一个取消它的按钮。现在真的有办法停止倾销或储蓄吗?我的感觉是进程一启动,不管怎样都会结束。
所以基本上,如果你有类似
的东西import pickle
data = 1
with open('./file', 'wb') as file:
pickle.dump(data, file, pickle.HIGHEST_PROTOCOL)
# or
data = pickle.load(file)
有什么方法可以阻止文件被(覆盖)写入吗?还是正在加载到内存中的数据?
一种方法是将您的数据集分成几块,并在每一步检查是否取消,例如:
import pickle
cancel = False
dataset_path = './dataset.pkl'
dataset = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
with open(dataset_path, 'wb') as handle:
for data in dataset:
if cancel:
break
pickle.dump(data, handle, pickle.HIGHEST_PROTOCOL)
# or
dataset = []
with open(dataset_path, 'rb') as handle:
while 1:
try:
dataset.append(pickle.load(handle))
except EOFError:
break
print(dataset)
输出:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]