小编Fed*_*kin的帖子

在 aiohttp 中执行请求时 await 和 async-with 之间有本质区别吗?

我的问题是关于做出回应的正确方式 aiohttp

官方 aiohttp 文档为我们提供了进行异步查询的示例:

session = aiohttp.ClientSession()

async with session.get('http://httpbin.org/get') as resp:
    print(resp.status)
    print(await resp.text())

await session.close()
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么这里的上下文管理器。我发现的只是__aexit__()方法等待resp.release()方法。但是文档还告诉我们resp.release()一般不需要等待。

这一切真的让我很困惑。

如果我发现下面的代码更具可读性并且不是那么嵌套,为什么要这样做?

session = aiohttp.ClientSession()

resp = await session.get('http://httpbin.org/get')
print(resp.status)
print(await resp.text())

# I finally have not get the essence of this method.
# I've tried both using and not using this method in my code,
# I've not found any difference in behaviour.
# await resp.release()

await session.close()
Run Code Online (Sandbox Code Playgroud)

我已经深入研究了aiohttp.ClientSession它的上下文管理器来源,但我没有发现任何可以澄清情况的东西。

最后,我的问题是:有什么区别?

python asynchronous async-await python-asyncio aiohttp

6
推荐指数
1
解决办法
193
查看次数

pygame.quit() 可靠吗?

我最近制作了一个小游戏,然后使用pyinstaller. 运行时,有两种方法可以关闭它。它是全屏的,所以不包括点击红色的 x 按钮,但基本上我有这个

if keys[pygame.K_ESCAPE]:
    pygame.quit()
Run Code Online (Sandbox Code Playgroud)

和这个

if exit_button.colliderect(cursor_rect) and click==1:
    pygame.quit()
Run Code Online (Sandbox Code Playgroud)

单击退出按钮后,游戏将毫无问题地关闭。光标紧接着在短时间内显示小加载符号。但是,当按下转义键时,窗口会关闭,但会弹出一条消息,“无法执行脚本”。我在 Windows 10 上,游戏是在 pycharm 上用 pygame 制作的,而 exe 是用pyinstaller --onefile -w game.py.

也许这是代码中的一个错误,但它在 IDE 中运行良好,我没有做任何不同的事情。我还可以确认它没有丢失图像或媒体。谢谢。

python pygame exe runtime-error pyinstaller

2
推荐指数
1
解决办法
111
查看次数