From b2aac097673afff8aa6dfcdf91c910383517f7d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bilal=20Ayakda=C5=9F?= <72439364+Bilal-AYAKDAS@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:05:11 +0300 Subject: [PATCH] Create awaitme_bilal_ayakdas.py --- Week05/awaitme_bilal_ayakdas.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Week05/awaitme_bilal_ayakdas.py diff --git a/Week05/awaitme_bilal_ayakdas.py b/Week05/awaitme_bilal_ayakdas.py new file mode 100644 index 00000000..1ab0c9c8 --- /dev/null +++ b/Week05/awaitme_bilal_ayakdas.py @@ -0,0 +1,22 @@ +import asyncio + +def awaitme(fn): + """ + Decorator to ensure synchronous and asynchronous functions can be awaited consistently. + + This decorator wraps a function, checking if it returns a coroutine. If so, it awaits the coroutine, + allowing synchronous functions to run as usual while enabling asynchronous support. + + :param fn: The function to be decorated. Can be synchronous or asynchronous. + :type fn: callable + :return: A coroutine function that awaits the result if needed. + :rtype: coroutine + + """ + + async def convert_to_coroutine(*args, **kwargs): + _func = fn(*args, **kwargs) + if asyncio.iscoroutine(_func): + return await _func + return _func + return convert_to_coroutine