python - 如何将 tqdm 进度条与进程同步
问题描述
我想创建一个 tqdm 进度条来显示某事的进度。假设我想生成一个 8192 位的 RSA 密钥。我想要一个可以显示是否完成的进度条。我怎么做?例如:
from Crypto.PublicKey import RSA
from tkinter import Tk, messagebox, simpledialog
import sys
from tqdm import tqdm
import halo
import multiprocessing as mp
root = Tk()
root.withdraw()
execute_shell_or_command = sys.executable
public_key_file = simpledialog.askstring('Key Generator',
'Enter the file you want to store the public key in (requires file extension)')
private_key_file = simpledialog.askstring('Key Generator',
'Enter the file you want to store the private key in (requires file '
'extension)')
key_num = simpledialog.askstring('Key Generator', 'Enter the key size, in bytes')
try:
if 'pythonw.exe' in execute_shell_or_command:
print('Generating key....')
key = RSA.generate(int(key_num))
else:
# p = mp.Process(target=tqdm())
spinner = halo.Halo(text='Generating key....', spinner='dots')
spinner.start()
# insert tqdm progressbar here
key = RSA.generate(int(key_num))
spinner.stop()
except ValueError:
print('Bruu, you actually thought that a letter is a number? Shame on you!')
else:
print('Opening (maybe creating) your files....')
file_priv = open(private_key_file, 'wb')
file_pub = open(public_key_file, 'wb')
print('Exporting and writing the private key....')
private_key = key.export_key()
file_priv.write(private_key)
file_priv.close()
print('Exporting and writing the public key....')
pub_key = key.publickey().export_key()
file_pub.write(pub_key)
file_pub.close()
print(f"Hooray! The private and public key has successfully been saved to "
f"{private_key_file} and {public_key_file}!")
解决方案
推荐阅读
- javascript - 将消息从 background.js 发送到 chrome 扩展中另一个页面的 content.js
- python - 切片 Dataframe 后 Pandas Multiindex 不受限制
- algorithm - 以下解决方案的时间和空间复杂度是多少?
- python - Python - 从多个 Zip 文件中提取 CSV 文件并合并数据
- azure - 在 ADF 中的 Lookup 和 For Each 活动中动态传递表名
- python - 如何遍历 ID 列表以每次为每个 ID 生成 URL?
- c++ - 简单的平方根
- javascript - 如何将javascript变量发送到django
- list - 通过 json 指定要添加到列表中的项目数
- javascript - 如何存储要在内部函数中使用的函数的参数