Last active
June 19, 2022 20:27
-
-
Save eldaduzman/f2fd5eb4f4e82eebd83f21b18fd98387 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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