OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Can I create a helper function for both async and sync environment?

  • Thread starter Thread starter PaleNeutron
  • Start date Start date
P

PaleNeutron

Guest
I'm trying to create helper function for my projects, but some legacy projects are in sync environment.

my helper function looks like:

Code:
def func_for_async_and_sync(session, data):
    statement = select(Obj).where(Obj.id == data['id'])
    # and more code
    if isinstance(session, AsyncSession):
        return await session.execute(statement)
    else:
        return session.execute(statement)

Of course it not work. How can I call AsyncSession.execute in normal function?
<p>I'm trying to create helper function for my projects, but some legacy projects are in sync environment.</p>
<p>my helper function looks like:</p>
<pre class="lang-py prettyprint-override"><code>def func_for_async_and_sync(session, data):
statement = select(Obj).where(Obj.id == data['id'])
# and more code
if isinstance(session, AsyncSession):
return await session.execute(statement)
else:
return session.execute(statement)
</code></pre>
<p>Of course it not work. How can I call <code>AsyncSession.execute</code> in normal function?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top