-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Allow asynchronous queries #263
Comments
This is an interesting request, but it's so big I'm not sure how to address it. Since I intend to keep compatibility with 2.6 for quite some time, I think it's not really possible. Also, my experience with "async" in python has been limited to gevent and I don't feel very comfortable with the new APIs ( Do you have any more thoughts or information to add? |
I don't, and I'm not expecting peewee to change any time soon. I am I am myself not yet comfortable with asyncio but since I'm forced to Le ven. 29 nov. 2013 05:42:40 CET, Charles Leifer a écrit :
|
Thanks for the message @sametmax. Based on my understanding of the new asyncio library, I'm going to go out on a limb here and say that I don't think peewee will be adding support.
Have a look at the examples to see how the structure of the application changes when you start using the |
I understand. Best solution would be to have the common code, such as And allow something like : from peewee import blabla # sync Or make peewee decoupled enough so that it's possible to build an async But I get it, it's a lot of work, and when you already have coded the Anyway, thanks for answering. Le ven. 29 nov. 2013 17:20:19 CET, Charles Leifer a écrit :
|
Just to add to this discussion, I've had positive experiences with the way Guido's NDB handles async operations. If in the future you are ever in need of any ideas, here are the docs: |
We can use Trollius for Py2/3 when needed. Much like asyncio, but it has friendly grammer for Py2( |
Thanks a bunch for the links - I will read up on them. |
I've just tried a flask+peewee app with uwsgi+gevent+psycogreen.gevent.patch_psycopg -- in theory, this should patch psycopg so that calling code (such as peewee) can use it as if it were still blocking, see https://bitbucket.org/dvarrazzo/psycogreen When I try this, peewee gives me:
Am I expecting too much? |
Strange...I've used psycopg2 with gevent and not had any issues. The code I used was similar to the monkeypatch you linked up @cpbotha . One thing you might check is that the gevent monkeypatch needs to be the first thing that happens. So the entry-point to your application would look like this: from gevent import monkey
monkey.patch_all()
from psycopg2_green_monkeypatch import whatever
whatever()
# here begins your actual code... |
Thanks for helping me with this! I already have in my wsgi.py (entry point for uwsgi):
With "normal" DB access (RESTful via browser), I see no issues. However, I do see the ProgrammingError exception when I do Anything else I could look at? (this is with peewee 2.2.3) |
Strange... I'm not sure what might be happening. |
Seems it has happened before: https://bitbucket.org/dvarrazzo/psycogreen-hg/issue/1/databaseerror-execute-used-with The explanation offered is issue would occur when two queries are done using two different cursors on the same database connection. Does that make sense? The reporter of that bug then wrote a blog post with a solution http://www.manasupo.com/2012/03/geventpsycopg2-execute-cannot-be-used.html where the simply do the psycopg2 monkey-patching before the fork. I don't understand why that fixed the problem in their case. In my case, the monkey patching is done before forking, if I may judge by the uwsgi log file (my "MONKEY PATCHING YEAH!" output appears once, before the the three worker processes report for duty):
I'm putting this here as a log for future travellers. Any tips would be welcome of course! |
In Momoko (Tornado wrapping of psycopg2), they had to build in more explicit handling of busy database connections to work around this issue: Tsumanga-Studios/momoko@e4752c9 This was supposed to be a short experiment to benchmark multi-process uwsgi+flask-peewee against multi-process+asnyc+flask-peewee. I think I should let it go, as they say. :) Thanks in any case! |
Are you using db = PostgresqlDatabase('foo', threadlocals=True) |
If I could fly over there and buy you a beer I would!! You even documented it: https://github.com/coleifer/flask-peewee/blob/master/docs/gevent.rst I don't understand why my searching didn't take me there, but I'm happy that you've solved it! (in my app.py, I added a 'threalocals' key to the DATABASE configuration dictionary. What impact does that setting have in a non-greened environment? Thanks again, |
In a non-greened environment if you were using a multi-threaded WSGI server, then your connections would be opened per-thread. |
I think we just need a separate package, let's say 'aiopeewee', providing asyncio interface. We're using peewee with Tornado+asyncio, so I think we'll start porting it shortly. Meanwhile we use 'run_in_executor' for performing slow requests asynchronously. |
And I'm not sure yet do we actually need porting or just a couple of wrappers and asyncio powered database backend classes. |
You will need more than that actually because the nature of the API E.G : when you access an attribute in peewee, it can fire an new query: You'll probably need to return a promise for very thing that should Now if you do that in a template : {{ product.shop.name }} Things get funny, because most template don't have a way to handle Plus, there are not just promises, but also deferred and futures, It's a lot of work, it's hard. But if you do it, peewee will be actually Unfortunatly, I don't know any way to do async with most DB without I would make it something la peewee.async, instead of a separate lib. It Le 26/09/2014 11:18, Alexey a écrit :
|
Hi everyone :) I've just published a kind of working proto: https://github.com/05bit/peewee-async I think basic async support may also be useful, we can deal with related objects by sending explicit prefetch queries. And yes, async queries are difficult to support in templates (without rewriting template engine), so I think it's more suitable for API services where we generally just need to serialise to JSON. |
Just published alpha v0.0.2 on PyPi and here's the docs: https://python-aiopeewee.readthedocs.org Interface seems working and simple one and I think it's now rather close to stable version. But internals don't really shine, I've started issue to discuss better integration with peewee: 05bit/peewee-async#1 |
Has anything happened since then? |
Use gevent |
Hard one, as peewee has been built on top of synchronous DB driver up untill then, but with Python 3.4 comming and shipping with asyncio + yield from, this can be an interesting for Python 3 users.
The text was updated successfully, but these errors were encountered: