Python 等同于 JavaScript 中的 Promise 是什么?
What is the Python equivalent of a Promise in JavaScript?
我试图了解什么是最好的代码结构或设计模式,可以执行以下操作:
有一个名为 get_data
的函数,它将启动一个套接字连接,并开始等待一个特定的套接字事件,该事件将 return 一些数据。获取数据后,才应 get_data
函数 returned.
所以在 JS 中它会像这样非常简单:
(请记住,此代码片段只是示例,并不意味着是有效代码)
const getData = async ()=>{
return new Promise((resolve, reject)=>{
const socket = socket()
socket.on("message", (data)=>{
resolve(data)
})
socket.connect("localhost")
});
}
const parseData = async () => {
const data = await getData()
// do something with the data
}
然而,对于 python,我完全不知道如何实现相同的结果。
我如何将其翻译成 python?这里会使用什么策略?
我现在唯一能想到的方法是这样的:
def get_data():
socket = socket()
my_data = None
@socket.event
def message(data):
nonlocal my_data
my_data = data
socket.connect("localhost")
while not my_data
time.sleep(0.3)
socket.disconnect()
return my_data
def parse_data():
data = get_data()
# do something with the data
如果您想将基于回调的 API 转换为 async
API,您可能正在寻找 asyncio.Future
.
稍微修改一下你的稻草人代码:
import asyncio
def get_data():
# create a future
future = asyncio.Future()
# create a socket
socket = socket()
# connect callbacks
@socket.on("message")
def on_message(data):
future.set_result(data)
@socket.on("error")
def on_error(error):
future.set_exception(error)
# create a background task that will communicate with the socket
# and invoke the callbacks
@asyncio.create_task
async def communicate():
socket.connect('localhost')
socket.communicate()
# return the future
return future
调用 get_data
将 return 一个对象,您可以 await
就像 async
函数调用一样:
async def parse_data():
data = await get_data()
# process further
我试图了解什么是最好的代码结构或设计模式,可以执行以下操作:
有一个名为 get_data
的函数,它将启动一个套接字连接,并开始等待一个特定的套接字事件,该事件将 return 一些数据。获取数据后,才应 get_data
函数 returned.
所以在 JS 中它会像这样非常简单:
(请记住,此代码片段只是示例,并不意味着是有效代码)
const getData = async ()=>{
return new Promise((resolve, reject)=>{
const socket = socket()
socket.on("message", (data)=>{
resolve(data)
})
socket.connect("localhost")
});
}
const parseData = async () => {
const data = await getData()
// do something with the data
}
然而,对于 python,我完全不知道如何实现相同的结果。
我如何将其翻译成 python?这里会使用什么策略?
我现在唯一能想到的方法是这样的:
def get_data():
socket = socket()
my_data = None
@socket.event
def message(data):
nonlocal my_data
my_data = data
socket.connect("localhost")
while not my_data
time.sleep(0.3)
socket.disconnect()
return my_data
def parse_data():
data = get_data()
# do something with the data
如果您想将基于回调的 API 转换为 async
API,您可能正在寻找 asyncio.Future
.
稍微修改一下你的稻草人代码:
import asyncio
def get_data():
# create a future
future = asyncio.Future()
# create a socket
socket = socket()
# connect callbacks
@socket.on("message")
def on_message(data):
future.set_result(data)
@socket.on("error")
def on_error(error):
future.set_exception(error)
# create a background task that will communicate with the socket
# and invoke the callbacks
@asyncio.create_task
async def communicate():
socket.connect('localhost')
socket.communicate()
# return the future
return future
调用 get_data
将 return 一个对象,您可以 await
就像 async
函数调用一样:
async def parse_data():
data = await get_data()
# process further