Skip to content

Commit

Permalink
Require transaction to be wrapped in asyncio task
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed May 1, 2016
1 parent 157fa77 commit f37960d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,10 +1274,12 @@ def __init__(self, *args, **kwargs):

class transaction:
"""Asynchronous context manager (`async with`), similar to
`peewee.transaction()`.
`peewee.transaction()`. Will start new `asyncio` task for
transaction if not started already.
"""
def __init__(self, db):
self.db = db
self.loop = db.loop

@asyncio.coroutine
def commit(self, begin=True):
Expand All @@ -1292,12 +1294,18 @@ def rollback(self, begin=True):
yield from _run_sql(self.db, 'BEGIN')

@asyncio.coroutine
def __aenter__(self):
def begin(self):
yield from self.db.push_transaction_async()

if self.db.transaction_depth_async() == 1:
yield from _run_sql(self.db, 'BEGIN')

@asyncio.coroutine
def __aenter__(self):
if asyncio.Task.current_task(loop=self.loop):
yield from self.begin()
else:
yield from self.loop.create_task(self.begin())
return self

@asyncio.coroutine
Expand Down Expand Up @@ -1402,8 +1410,16 @@ def _execute_query_async(query):


class TaskLocals:
"""Simple `dict` wrapper to get and set values on
per `asyncio` task basis.
"""Simple `dict` wrapper to get and set values on per `asyncio`
task basis.
The idea is similar to thread-local data, but actually *much* simpler.
It's no more than a "sugar" class. Use `get()` and `set()` method like
you would to for `dict` but values will be get and set in the context
of currently running `asyncio` task.
When task is done, all saved values is removed from stored data.
"""
def __init__(self, loop):
self.loop = loop
Expand Down

0 comments on commit f37960d

Please sign in to comment.