Pyscript异步请求需要强制等待数据。

huangapple go评论70阅读模式
英文:

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:

&lt;!-- index.html --&gt;
&lt;py-config&gt;
    packages = [&quot;requests&quot;, &quot;pyodide-http&quot;]
&lt;/py-config&gt;

&lt;py-script src=&quot;demo.py&quot;&gt;&lt;/py-script&gt;
&lt;button py-click=&quot;run_comparison()&quot;&gt;Get and Print Data&lt;/button&gt;
#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(&quot;https://reqres.in/api/users?page=2&quot;)
    return response.json()

def run_comparison(*ags, **kws):
    json_data = get_tp_data()

    #Do something interesting with the data
    print(f&quot;The highest id is {max(item[&#39;id&#39;] for item in json_data[&#39;data&#39;])}&quot;)

huangapple
  • 本文由 发表于 2023年5月30日 05:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定