How can I hash a password in Tornado with minimal blocking?

只谈情不闲聊 提交于 2019-12-05 02:24:31

问题


I'm using PBKDF2, but this applies equally to BCrypt.

Hashing a password with a reasonable number of iterations can easily block for 0.5 seconds. What is a lightweight way to take this out of process? I'm reluctant to setup something like Celery or Gearman just for this operation.


回答1:


You could use a thread. This will not block tornado. Say that you have a handler that hashes passwords. Then the two relevant methods might look like this:

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

You could either wait for the thread to finish in the on_message method then write the response, or if you don't need to send a response then just let it finish and save the results in the hash_password method.

If you do wait for the thread to finish you have to be careful how you do it. time.sleep will block so you will want to use tornado's non blocking wait instead.



来源:https://stackoverflow.com/questions/13103049/how-can-i-hash-a-password-in-tornado-with-minimal-blocking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!