我正在使用 PBKDF2 ,但这同样适用于 BCrypt。
通过合理的迭代次数对密码进行哈希处理可以轻松阻塞 0.5 秒。什么是将其排除在外的轻量级方法?我不愿意为此操作设置 Celery 或 Gearman 之类的东西。
最佳答案
您可以使用线程。这不会阻止 Tornado 。假设您有一个散列密码的处理程序。那么两个相关的方法可能如下所示:
import threading
def on_message(self, message):
# pull out user and password from message somehow
thread = threading.Thread(target=self.hash_password, args=(user, password))
thread.start()
def hash_password(self, user, password):
# do the hash and save to db or check against hashed password
您可以在 on_message
方法中等待线程完成,然后写入响应,或者如果您不需要发送响应,则让它完成并将结果保存在hash_password
方法。
如果您确实要等待线程完成,则必须小心操作。 time.sleep
会阻塞,所以你会想改用 tornado 的非阻塞等待。
关于python - 如何在 Tornado 中以最少的阻塞散列密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103049/