-
-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to run tortoise with quart async rest api #119
Comments
I have not used Quart before, I know with aiohttp you have a init function where you can add async initialisers to use. It also contained the object state in the app, so you had no other globals. That code looks like it should work, I will have to try it locally. |
Here is a working sample app. (It probably is un-quark-ish as it is my first Quark app) I hope this helps 😄 import asyncio
import json
from random import choice
from quart import Quart
from tortoise import Tortoise, fields
from tortoise.models import Model
STATUSES = ["New", "Old", "Gone"]
app = Quart(__name__)
class Users(Model):
id = fields.IntField(pk=True)
status = fields.CharField(20)
def __str__(self):
return "User {}: {}".format(self.id, self.status)
class Workers(Model):
id = fields.IntField(pk=True)
status = fields.CharField(20)
def __str__(self):
return "Worker {}: {}".format(self.id, self.status)
@app.before_serving
async def init_orm():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
@app.after_serving
async def close_orm():
await Tortoise.close_connections()
@app.route("/")
async def list_all():
users, workers = await asyncio.gather(Users.all(), Workers.all())
return json.dumps(
{
"users": [str(user) for user in users],
"workers": [str(worker) for worker in workers],
},
indent=4,
)
@app.route("/user")
async def add_user():
user = await Users.create(status=choice(STATUSES))
return str(user)
@app.route("/worker")
async def add_worker():
worker = await Workers.create(status=choice(STATUSES))
return str(worker)
if __name__ == "__main__":
app.run(port=5000) |
@grigi Thanks for your quick response! :)
if so maybe we have inconsistency with the environment ...
Again thanks for your feedback :) |
It ran for me. I used py3.6.6 and So I should try this on Py3.7 I suppose they changed their api. And I see py3.6 is limited to the older version. |
thanks I will check it out ! |
I tested it on py37 and the following pins (latest of everything):
And it worked fine with no changes at all. The only outstanding difference is that my example was using SQLite instead of PostgreSQL. |
very strange ... cause i just run it using python3.6 and the integration worked! but when comeback to python3.7 the error raised aggain .. maybe my virtual env is massed up or something I will create new one and check it again Thakns @grigi :) |
Whilst playing with a more complete quart sample, I discovered that using We should put a warning about usage of it. |
I updated the sample at #121 could you review it for sanity? |
Describe the bug
Hi,
I have simple tortoise example that work fine when i create the python event loop and run it.
but when i run Quart API server I get strange error, I braked my head trying to figure out what is the issue
(I'm running python3.7)
The script I ran is below:
To Reproduce
run one time the "run_quart" see the error
then run "run_tortoise" see the run succeed
Expected behavior
tortoise ORM will use the Quart event loop
Actual behavior
Tortoise raise "NotADirectoryError" error
Additional context
This is my first time using Tortoise ORM and its looking very cool, I will appreciate any help with this issue
Thanks in advanced!
The text was updated successfully, but these errors were encountered: