Pygame と asyncio を使用してネットワーク ゲームを作成しようとしていますが、読み取りのハングを回避する方法がわかりません。クライアント用の私のコードは次のとおりです。
@asyncio.coroutine
def handle_client():
print("Connected!")
reader, writer = yield from asyncio.open_connection('localhost', 8000)
while True:
mouse_up = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
mouse_up = True
if mouse_up:
print("Writing")
writer.write(b"Mouse up")
print("Waiting to read")
line = yield from reader.read(2**12)
print(line.decode())
writer.close()
これは行にかかっていますline = yield from reader.read(2**12)
。以前は、asyncio のポイントはノンブロッキングであると考えていたので、読み取るデータがない場合は実行を継続するだけでした。そうではないことがわかりました。
asyncio ネットワーク コードを Pygame の描画およびイベント コードと統合するにはどうすればよいですか?