File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
2+ import os
3+ from time import perf_counter
4+
5+ from libsql_client import create_client
6+
7+ async def insert (client , t : str , s : str ):
8+ return await client .execute (f"INSERT INTO { t } VALUES ('{ s } ')" )
9+
10+ async def sync_demo (client ):
11+ initial = perf_counter ()
12+ for i in range (20 ):
13+ await insert (client , "sync" , f"{ i } " )
14+ print (f"Sync code took { perf_counter () - initial } seconds to run." )
15+
16+ async def async_demo (client ):
17+ initial = perf_counter ()
18+ requests = [insert (client , "async" , f"{ i } " ) for i in range (20 )]
19+ await asyncio .gather (* requests )
20+ print (f"Async code took { perf_counter () - initial } seconds to run." )
21+
22+ async def main ():
23+ url = os .getenv ("TURSO_URL" , "" )
24+ async with create_client (url ) as client :
25+ await client .execute ("CREATE TABLE IF NOT EXISTS sync (name TEXT)" )
26+ await client .execute ("CREATE TABLE IF NOT EXISTS async (name TEXT)" )
27+
28+ await sync_demo (client )
29+ await async_demo (client )
30+
31+
32+
33+ asyncio .run (main ())
Original file line number Diff line number Diff line change 1+ import asyncio
2+ import os
3+ from libsql_client .dbapi2 import connect
4+ from time import perf_counter
5+
6+ async def insert (conn , t : str , s : str ):
7+ return await conn .execute (f"INSERT INTO { t } VALUES ('{ s } ')" )
8+
9+ async def sync_demo (conn ):
10+ initial = perf_counter ()
11+ for i in range (20 ):
12+ await insert (conn , "sync" , f"{ i } " )
13+ await conn .commit ()
14+ print (f"Sync code took { perf_counter () - initial } seconds to run." )
15+
16+ async def async_demo (conn ):
17+ initial = perf_counter ()
18+ requests = [insert (conn , "async" , f"{ i } " ) for i in range (20 )]
19+ await asyncio .gather (* requests )
20+ await conn .commit ()
21+ print (f"Async code took { perf_counter () - initial } seconds to run." )
22+
23+ async def main ():
24+ url = os .getenv ("TURSO_URL" , "" )
25+ conn = connect (url , uri = True )
26+
27+ await conn .execute ("CREATE TABLE IF NOT EXISTS dbapi2_sync (t TEXT)" )
28+ await conn .execute ("CREATE TABLE IF NOT EXISTS dbapi2_async (t TEXT)" )
29+
30+ await sync_demo (conn )
31+ await async_demo (conn )
32+
33+ asyncio .run (main ())
You can’t perform that action at this time.
0 commit comments