Skip to content

Commit

Permalink
Add new simple TaskLocals (draft)
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed May 1, 2016
1 parent d9c6db7 commit b5b2837
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,3 +1396,50 @@ def _execute_query_async(query):
"""Execute query and return cursor object.
"""
return (yield from _run_sql(query.database, *query.sql()))


class TaskLocals:
"""Simple `dict` wrapper to get and set values on
per `asyncio` task basis.
"""
def __init__(self, loop):
self.loop = loop
self.data = {}

def get(self, key, *val):
"""Get value stored for current running task. Optionally
you may provide the default value. Raises `KeyError` when
can't get the value and no default one is provided.
"""
data = self.get_data()
if data:
return data.get(key, *val)
elif len(val):
return val[0]
else:
raise KeyError(key)

def set(self, key, val):
"""Set value stored for current running task.
"""
data = self.get_data()
if data:
data[key] = val
else:
raise RuntimeError("No task is currently running")

def get_data(self):
"""Get dict stored for current running task.
"""
task = asyncio.Task.current_task(loop=self.loop)
if task:
taks_id = id(task)
if not taks_id in self.data:
self.data[taks_id] = {}
task.add_done_callback(self.pop_data)
return self.data[taks_id]

def pop_data(self, task):
"""Delete data for task from stored data dict.
"""
self.data.pop(id(task), None)

0 comments on commit b5b2837

Please sign in to comment.