Skip to content

Commit

Permalink
Fix async functions should not call open()
Browse files Browse the repository at this point in the history
% [`ruff --select=ASYNC .`](https://beta.ruff.rs/docs/rules/open-sleep-or-subprocess-in-async-function)
2	ASYNC101	[ ] Async functions should not call `open`, `time.sleep`, or `subprocess` methods
  • Loading branch information
cclauss committed Jul 20, 2023
1 parent 985c448 commit aedb0bb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions examples/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import os
from typing import List, Union

import aiofiles

from aiohttp import web

WS_FILE = os.path.join(os.path.dirname(__file__), "websocket.html")
Expand All @@ -18,8 +20,8 @@ async def wshandler(request: web.Request) -> Union[web.WebSocketResponse, web.Re
resp = web.WebSocketResponse()
available = resp.can_prepare(request)
if not available:
with open(WS_FILE, "rb") as fp:
return web.Response(body=fp.read(), content_type="text/html")
async with aiofiles.open(WS_FILE, "rb") as fp:
return web.Response(body=await fp.read(), content_type="text/html")

await resp.prepare(request)

Expand Down
1 change: 1 addition & 0 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ mypy==0.982; implementation_name=="cpython"
pre-commit==2.17.0
pytest==6.2.5
slotscheck==0.8.0
types-aiofiles==23.1.0.4
uvloop==0.17.0; platform_system!="Windows"
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r base.txt
aiofiles
Brotli==1.0.9
coverage==6.4.2
cryptography==36.0.1; platform_machine!="i686" # no 32-bit wheels; no python 3.9 wheels yet
Expand Down
6 changes: 4 additions & 2 deletions tools/bench-asyncio-write.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import signal
from typing import List, Tuple

import aiofiles

PORT = 8888

server = os.fork()
Expand Down Expand Up @@ -123,9 +125,9 @@ async def bench(job_title, w, body, base=None):
base = await bench(t, writes[0], c)
for w in writes[1:]:
await bench("", w, c, base)
with open("bench.md", "w") as f:
async with aiofiles.open("bench.md", "w") as f:
for line in res:
f.write("| {} |\n".format(" | ".join(line)))
await f.write("| {} |\n".format(" | ".join(line)))


loop = asyncio.get_event_loop()
Expand Down

0 comments on commit aedb0bb

Please sign in to comment.