Skip to content

Instantly share code, notes, and snippets.

@eldaduzman
Last active June 19, 2022 20:27
Show Gist options
  • Select an option

  • Save eldaduzman/f2fd5eb4f4e82eebd83f21b18fd98387 to your computer and use it in GitHub Desktop.

Select an option

Save eldaduzman/f2fd5eb4f4e82eebd83f21b18fd98387 to your computer and use it in GitHub Desktop.
import asyncio
from tortoise import Tortoise, run_async, connections
from tortoise.models import Model
from tortoise import fields
import random
import time
class Employees(Model):
ID = fields.IntField(pk=True)
NAME = fields.TextField()
AGE = fields.IntField()
def __str__(self):
return self.NAME
async def init():
await Tortoise.init(db_url="sqlite://db3.sqlite3", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
async def main(loop):
await init()
cors = []
try:
for i in range(1, 30):
age = random.randint(25, 45)
cors.append(loop.create_task(Employees(NAME=f"employ{i}", AGE=age).save()))
cors.append(loop.create_task(asyncio.sleep(10)))
await asyncio.gather(*cors)
finally:
await connections.close_all(discard=True)
start = time.time()
main_loop = asyncio.get_event_loop()
main_loop.run_until_complete(main(main_loop))
end = time.time()
print(f"total_time= {end-start:.4} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment