web-dev-qa-db-ja.com

非同期でファイルを書き込む

クライアントプロセスから入力ファイルパスと出力パスを非同期で受信するサーバープロセスを作成しようとしています。サーバーはデータベースに依存する変換をいくつか実行しますが、簡単にするために、すべてを大文字にするだけであると仮定しましょう。サーバーのおもちゃの例を次に示します。

import asyncio
import aiofiles as aiof
import logging
import sys


ADDRESS = ("localhost", 10000)

logging.basicConfig(level=logging.DEBUG,
                    format="%(name)s: %(message)s",
                    stream=sys.stderr)

log = logging.getLogger("main")
loop = asyncio.get_event_loop()


async def server(reader, writer):
    log = logging.getLogger("process at {}:{}".format(*ADDRESS))
    paths = await reader.read()
    in_fp, out_fp = paths.splitlines()
    log.debug("connection accepted")
    log.debug("processing file {!r}, writing output to {!r}".format(in_fp, out_fp))
    async with aiof.open(in_fp, loop=loop) as inp, aiof.open(out_fp, "w", loop=loop) as out:
        async for line in inp:
            out.write(line.upper())
        out.flush()
    writer.write(b"done")
    await writer.drain()
    log.debug("closing")
    writer.close()
    return


factory = asyncio.start_server(server, *ADDRESS)
server = loop.run_until_complete(factory)
log.debug("starting up on {} port {}".format(*ADDRESS))

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    log.debug("closing server")
    server.close()
    loop.run_until_complete(server.wait_closed())
    log.debug("closing event loop")
    loop.close()

クライアント:

import asyncio
import logging
import sys
import random

ADDRESS = ("localhost", 10000)
MESSAGES = ["/path/to/a/big/file.txt\n", 
            "/output/file_{}.txt\n".format(random.randint(0, 99999))]

logging.basicConfig(level=logging.DEBUG,
                    format="%(name)s: %(message)s",
                    stream=sys.stderr)

log = logging.getLogger("main")
loop = asyncio.get_event_loop()

async def client(address, messages):
    log = logging.getLogger("client")
    log.debug("connecting to {} port {}".format(*address))
    reader, writer = await asyncio.open_connection(*address)
    writer.writelines([bytes(line, "utf8") for line in messages])
    if writer.can_write_eof():
        writer.write_eof()
    await writer.drain()

    log.debug("waiting for response")
    response = await reader.read()
    log.debug("received {!r}".format(response))
    writer.close()
    return


try:
    loop.run_until_complete(client(ADDRESS, MESSAGES))
finally:
    log.debug("closing event loop")
    loop.close()

サーバーと複数のクライアントを一度にアクティブ化しました。サーバーのログ:

asyncio: Using selector: KqueueSelector
main: starting up on localhost port 10000
process at localhost:10000: connection accepted
process at localhost:10000: processing file b'/path/to/a/big/file.txt', writing output to b'/output/file_79609.txt'
process at localhost:10000: connection accepted
process at localhost:10000: processing file b'/path/to/a/big/file.txt', writing output to b'/output/file_68917.txt'
process at localhost:10000: connection accepted
process at localhost:10000: processing file b'/path/to/a/big/file.txt', writing output to b'/output/file_2439.txt'
process at localhost:10000: closing
process at localhost:10000: closing
process at localhost:10000: closing

すべてのクライアントはこれを印刷します:

asyncio: Using selector: KqueueSelector
client: connecting to localhost port 10000
client: waiting for response
client: received b'done'
main: closing event loop

出力ファイルは作成されますが、空のままです。私は彼らが洗い流されていないと信じています。どうすれば修正できますか?

6
Eli Korvigo

out.write()およびout.flush()の前にawaitがありません。

import asyncio
from pathlib import Path

import aiofiles as aiof

FILENAME = "foo.txt"


async def bad():
    async with aiof.open(FILENAME, "w") as out:
        out.write("hello world")
        out.flush()
    print("done")


async def good():
    async with aiof.open(FILENAME, "w") as out:
        await out.write("hello world")
        await out.flush()
    print("done")


loop = asyncio.get_event_loop()

server = loop.run_until_complete(bad())
print(Path(FILENAME).stat().st_size)  # prints 0

server = loop.run_until_complete(good())
print(Path(FILENAME).stat().st_size)  # prints 11

ただし、aiofileをスキップし、通常の同期ディスクI/Oを使用し、ネットワークアクティビティのために非同期を維持することを強くお勧めします。

with open(file, "w"):  # regular file I/O
    async for s in network_request():  # asyncio for slow network work. measure it!
        f.write(s) # should be really quick, measure it!
3
Udi