In python class, when you have to mix synchronous and asynchronous functions, what is the best practice ?
first option:
Class My_Class:
def myfct_sync(...)
async def myfct_async(...)
second option: prefix with a 'a' character, like __iter__ and __aiter__
Class My_Class:
def myfct(...)
async def amyfct(...)
third option: separate in two classes
Class My_Class:
def myfct(...)
Class Async_My_Class:
async def myfct(...)
All classes in Python should be
PascalCase. This is detailed in PEP 8 - Style Guide for Python. Aside from that, I am not aware of any specificasyncionaming conventions.For asynchronous related classes, it is up to you and your code base. In a purely
asynciocodebase, I don't think there is any added benefit by putting the wordAsyncsomewhere in the class names. However, in a code base whereasynciocode will be mixed in with synchronous code, I think there is indeed a benefit to prefixing classes withAsync. For example, say that you have a synchronous thread communicating with anasyncioevent loop. Inside the event loop, you might create anInboxclass to communicate between the various coroutines and tasks running on the event loop. It makes sense to name the classAsyncInboxinstead of justInboxto make it clear that it is intended to be used inside an event loop.For methods, that is again up to you. If you look at the core module for
asyncio.Queue, there is a mix of synchronous (get_nowait,put_nowait) and asynchronous methods (get,put). For the most part, a linter for your IDE such as Pylance should make it clear enough whether a method or function returns a coroutine or not. Also, coroutines are required to use theasynckeyword. So generally speaking and as opposed to my recommendation for classes, I think it doesn't make much sense to add the wordasyncinto a function or method.Where it does make sense to me is for when a class (and thus methods) or a module (and thus functions) expose two versions of a function, one synchronous and one asynchronous. In such a special case, one could consider putting
_asyncas a suffix. But that still depends. For example, theasyncio.Queueclass above doesn't do that because theget_nowaitandput_nowaitare effectively non-blocking functions. If they were, then naming themget_syncandput_syncOR leaving them asgetandputand naming the asynchronous blocking versions asget_asyncandput_asyncwould be some options.