首页 > 解决方案 > 如何将 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}!")

标签: pythontqdm

解决方案


推荐阅读