0
https://github.com/saucelabs/monocle單片機是否使用事件驅動編程的一個壞例子?
下面的代碼給出標準事件驅動編程的一個例子:
def get_cmd(conn): conn.read_until("\n", callback=handle_cmd) def handle_cmd(conn, cmd): if cmd.type == "get-address": # keep track of the conn so we can write the response back! def callback(result): handle_user_query_result(conn, result) db.query(cmd.username, callback) else: conn.write("unknown command") def handle_user_query_result(conn, user): conn.write(user.address)
雖然我不明白爲什麼需要封閉。 「跟蹤conn」與第一個函數「get_cmd」的工作方式相矛盾。另外,不要事件驅動框架通常讓你傳遞參數?
我猜這將是一個更合法的例子:
def get_cmd(conn): conn.read_until("\n", callback=handle_cmd) def handle_cmd(conn, cmd): if cmd.type == "get-address": db.query(cmd.username, callback=handle_user_query_result, params=conn) else: conn.write("unknown command") def handle_user_query_result(result, params): user = result conn = params conn.write(user.address)
難道我錯了嗎?