alec_djinn
alec_djinn

Reputation: 10819

What is the obvious way of making a simple async socket chat server in Python 3?

I am getting my mind around asyncio and I would like to write a simple socked-based chat server. I have been looking around to understand what is the best way to approach this task.

Unfortunately, it appears that there is no "obvious way to do it". I find myself unable to decide, for example, whether to use @asincio.coroutine or async def, whether to use asyncio.Protocol of .start_server or .asynchat. or something else?
Does it actually make any difference in practice?

Could you please help enlight me on this?

Upvotes: 0

Views: 448

Answers (1)

user4815162342
user4815162342

Reputation: 155436

I find myself unable to decide, for example, whether to use @asincio.coroutine or async def

Use async def. You need @asyncio.coroutine only if you want to support pre-3.5 Python, or perhaps if you are writing very specialized implementations of low-level coroutines.

whether to use asyncio.Protocol of .start_server

Go with start_server. The stream-based APIs expose coroutines to the API user and are how asyncio is meant to be interfaced with by the majority of application code. Understanding transports and protocols is important if you are digging into the details, or if you are writing an asyncio-based library that implements a whole protocol.

or .asynchat

According to its own documentation, asynchat is deprecated.

Upvotes: 2

Related Questions