如何在 Tornado 中以最少的阻塞对密码进行哈希处理?

2024-04-14

我在用着PBKDF2 http://en.wikipedia.org/wiki/PBKDF2,但这同样适用于 BCrypt。

通过合理的迭代次数对密码进行哈希处理可以轻松阻塞 0.5 秒。有什么轻量级的方法可以将其从流程中删除?我不愿意仅仅为了这个操作而设置像 Celery 或 Gearman 这样的东西。


你可以使用线程。这不会阻挡龙卷风。假设您有一个对密码进行哈希处理的处理程序。那么两个相关的方法可能如下所示:

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会阻塞,所以你需要使用龙卷风的非阻塞等待。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Tornado 中以最少的阻塞对密码进行哈希处理? 的相关文章

随机推荐