英文:
Pyscript async requests need force wait for the data
问题
I am following the requests tutorial on pyscript here:
https://docs.pyscript.net/latest/tutorials/requests.html
I am not familiar with asyncio and there is no fuller Pyscript impementation to guide my needs on waiting for a request to complete and return data.
Reading up on asyncio has not helped me so far.
I sucessfully get data from the server.
I need a way to force wait until the data is obtained from the server.
async def get_tp_data():
# request() is a wrapper for pyfetch - see https://docs.pyscript.net/latest/tutorials/requests.html
response = await request("https: the server site)", method="GET")
#print(f"GET request=> status:{response.status}"), json:{await response.json()}"
tp_response = await response.json()
tp_expansion = tp_response['expansion']
tp_extension = tp_expansion['extension']
tp_contains = tp_expansion['contains']
print(tp_expansion['contains']) # Sucessfully gets the data
return await tp_contains
tp_contains = asyncio.create_task(get_tp_data())
def run_comparison(*ags, **kws):
# Code here is called by index.html button press, this works
# Do stuff with tp_contains
英文:
I am following the requests tutorial on pyscript here:
https://docs.pyscript.net/latest/tutorials/requests.html
I am not familiar with asyncio and there is no fuller Pyscript impementation to guide my needs on waiting for a request to complete and return data.
Reading up on asyncio has not helped me so far.
I sucessfully get data from the server.
I need a way to force wait until the data is obtained from the server.
async def get_tp_data():
# request() is a wrapper for pyfetch - see https://docs.pyscript.net/latest/tutorials/requests.html
response = await request("https: the server site)", method="GET")
#print(f"GET request=> status:{response.status}"), json:{await response.json()}")
tp_response = await response.json()
tp_expansion = tp_response['expansion']
tp_extension = tp_expansion['extension']
tp_contains = tp_expansion['contains']
print(tp_expansion['contains']) # Sucessfully gets the data
return await tp_contains
tp_contains = asyncio.create_task(get_tp_data())
def run_comparison(*ags, **kws):
# Code here is called by index.html button press, this works
# Do stuff with tp_contains
答案1
得分: 1
根据语法和您提到的asyncio以及使用响应对象,我猜测您正在按照PyScript文档中的其他HTTP请求教程进行操作。您提供的链接使用pyodide-http包来修补requests包,并且不需要asyncio。我认为您提供的教程实际上更适合小型API请求,不太可能长时间阻塞,因为它避免了对asyncio的需求。
通过使用pyodide_http.patch_all()
,requests包基本上可以正常工作。以下是您的代码的简化版本,使用一个API测试端点:
<!-- index.html -->
<py-config>
packages = ["requests", "pyodide-http"]
</py-config>
<py-script src="demo.py"></py-script>
<button py-click="run_comparison()">获取并打印数据</button>
# demo.py
import pyodide_http
import requests
# 修补Requests库以使其与Pyscript兼容
pyodide_http.patch_all()
def get_tp_data():
response = requests.get("https://reqres.in/api/users?page=2")
return response.json()
def run_comparison(*args, **kwargs):
json_data = get_tp_data()
# 对数据执行一些有趣的操作
print(f"最高的ID是{max(item['id'] for item in json_data['data'])}")
这是您要求的翻译部分。
英文:
From the syntax and the fact that you mention asyncio and use the response object, I'm guessing you're following the other HTTP requests tutorial from the PyScript docs. The one you linked uses the pyodide-http package to patch the the requests package, and does not require asyncio. I think the tutorial you linked is actually the better route for small API requests that are unlikely to block for long, since it avoids the need for asyncio.
By utilizing pyodide_http.patch_all()
, the requests package mostly just works. Here's a slimmed-down version of your code, using an API-testing endpoint:
<!-- index.html -->
<py-config>
packages = ["requests", "pyodide-http"]
</py-config>
<py-script src="demo.py"></py-script>
<button py-click="run_comparison()">Get and Print Data</button>
#demo.py
import pyodide_http
import requests
# Patch the Requests library so it works with Pyscript
pyodide_http.patch_all()
def get_tp_data():
response = requests.get("https://reqres.in/api/users?page=2")
return response.json()
def run_comparison(*ags, **kws):
json_data = get_tp_data()
#Do something interesting with the data
print(f"The highest id is {max(item['id'] for item in json_data['data'])}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论