mirror of
https://github.com/vincentbernat/i3wm-configuration.git
synced 2025-07-27 14:24:56 +02:00
i3-companion: move all state inside fn.worker namespace
This is cleaner than putting arbitrary attributes inside a task. The scheduling will be done later, so it is safe to put put everything inside the namespace object and avoid polluting the function namespace.
This commit is contained in:
parent
b62f747837
commit
b6354b1256
1 changed files with 18 additions and 14 deletions
|
@ -16,6 +16,7 @@ import re
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import types
|
||||||
|
|
||||||
import i3ipc
|
import i3ipc
|
||||||
from i3ipc.aio import Connection
|
from i3ipc.aio import Connection
|
||||||
|
@ -106,16 +107,19 @@ def dampen(sleep, *, unless=None, retry=0):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(fn):
|
def decorator(fn):
|
||||||
async def worker(urgent):
|
async def worker():
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
# Wait for an urgent work or until sleep is elapsed
|
# Wait for an urgent work or until sleep is elapsed
|
||||||
await asyncio.wait_for(urgent.wait(), timeout=sleep)
|
await asyncio.wait_for(
|
||||||
|
fn.worker.urgent.wait(), timeout=sleep
|
||||||
|
)
|
||||||
|
logger.debug(f"urgent work received for {fn}")
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
pass
|
pass
|
||||||
retry, args, kwargs = fn.queue
|
retry, args, kwargs = fn.worker.queue
|
||||||
fn.queue = None
|
fn.worker.queue = None
|
||||||
urgent.clear()
|
fn.worker.urgent.clear()
|
||||||
|
|
||||||
# Execute the work
|
# Execute the work
|
||||||
logger.debug(f"execute work for {fn}")
|
logger.debug(f"execute work for {fn}")
|
||||||
|
@ -133,17 +137,17 @@ def dampen(sleep, *, unless=None, retry=0):
|
||||||
)
|
)
|
||||||
|
|
||||||
# Retry, unless we have something already scheduled
|
# Retry, unless we have something already scheduled
|
||||||
if fn.queue is not None:
|
if fn.worker.queue is not None:
|
||||||
logger.debug(f"retry now with queued event for {fn}")
|
logger.debug(f"retry now with queued event for {fn}")
|
||||||
urgent.set()
|
fn.worker.urgent.set()
|
||||||
continue
|
continue
|
||||||
logger.debug(f"reschedule retry for {fn}")
|
logger.debug(f"reschedule retry for {fn}")
|
||||||
fn.queue = (retry, args, kwargs)
|
fn.worker.queue = (retry, args, kwargs)
|
||||||
if unless is not None and unless(*args, **kwargs):
|
if unless is not None and unless(*args, **kwargs):
|
||||||
logger.debug(f"wake up now for retry of {fn}")
|
logger.debug(f"wake up now for retry of {fn}")
|
||||||
urgent.set()
|
fn.worker.urgent.set()
|
||||||
# Do we still have something to do?
|
# Do we still have something to do?
|
||||||
if fn.queue is None:
|
if fn.worker.queue is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
# No more work
|
# No more work
|
||||||
|
@ -152,12 +156,12 @@ def dampen(sleep, *, unless=None, retry=0):
|
||||||
|
|
||||||
@functools.wraps(fn)
|
@functools.wraps(fn)
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
fn.queue = (retry, args, kwargs)
|
|
||||||
if fn.worker is None:
|
if fn.worker is None:
|
||||||
logger.debug(f"create new worker for {fn}")
|
logger.debug(f"create new worker for {fn}")
|
||||||
urgent = asyncio.Event()
|
fn.worker = types.SimpleNamespace()
|
||||||
fn.worker = asyncio.create_task(worker(urgent))
|
fn.worker.task = asyncio.create_task(worker())
|
||||||
fn.worker.urgent = urgent
|
fn.worker.urgent = asyncio.Event()
|
||||||
|
fn.worker.queue = (retry, args, kwargs)
|
||||||
else:
|
else:
|
||||||
logger.debug(f"enqueue new work for {fn}")
|
logger.debug(f"enqueue new work for {fn}")
|
||||||
if unless is not None and unless(*args, **kwargs):
|
if unless is not None and unless(*args, **kwargs):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue