test = signal('test') test.connect(func_we_want_to_execute_when_signal_is_called)
回到如何调用信号的问题。在 Blinker 中,当我们在信号对象上调用 send() 时,会执行信号处理程序。所以对于我们的 Bài kiểm tra 信号,语法就是:
test.send()
khi test.send() 被调用时 func_we_want_to_execute_when_signal_is_call 会执行。希望 FP 文档中的这个示例现在更有意义:
def login_view(req): username = req.form.get('username') # Your authentication here.
# Notice our signal (identity_changed) is being called (identity_changed.send()) # What function is being called? Principal._on_identity_changed() identity_changed.send(app, identity=Identity(username))
# We're setting up our signal (identity_loaded) # to execute the function below (on_identity_loaded) # when we call our signal (identity_loaded.send()) # which is called in Principal._set_thread_identity() @identity_loaded.connect def on_identity_loaded(sender, identity): # Get the user information from the db user = db.get(identity.name)
# Update the roles that a user can provide for role in user.roles: identity.provides.add(RoleNeed(role.name))
# Save the user somewhere so we only look it up once identity.user = user
Tôi là một lập trình viên xuất sắc, rất giỏi!