如何在Tornado中使用最小阻塞来密码密码?

时间:2012-10-27 18:39:33

标签: python asynchronous tornado bcrypt pbkdf2

我正在使用PBKDF2,但这同样适用于BCrypt。

以合理的迭代次数散列密码可以轻松阻止0.5秒。什么是轻量级的方法来解决这个问题?我不愿意为此操作设置像Celery或Gearman这样的东西。

1 个答案:

答案 0 :(得分:6)

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

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方法中完成然后编写响应,或者如果您不需要发送响应,那么只需让它完成并将结果保存在{{1 }} 方法。

如果您确实等待线程完成,则必须小心如何执行此操作。 hash_password会阻止,所以你会想要使用龙卷风的非阻塞等待。