Skip to content

Commit

Permalink
updated cancelled signal for qtcoro
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed May 8, 2023
1 parent d20c2ac commit e8e429e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion koil/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def unkoil(coro, *args, **kwargs):
if not loop or loop0 == loop:
return coro(
*args, **kwargs
) # We are running in an event loop so we can just return the coroutine
) # We are running in an event doop so we can just return the coroutine

except RuntimeError:
pass
Expand Down
26 changes: 10 additions & 16 deletions koil/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from koil.vars import current_loop
import uuid

logger = logging.getLogger(__name__)


Expand All @@ -36,6 +37,7 @@ def get_receiver_length(qobject, qsignal, callstring):

class QtFuture:
def __init__(self):
self.id = uuid.uuid4().hex
self.loop = asyncio.get_event_loop()
self.aiofuture = asyncio.Future()
self.iscancelled = False
Expand Down Expand Up @@ -98,7 +100,7 @@ async def __anext__(self):

class QtCoro(QtCore.QObject, Generic[T, P]):
called = QtCore.Signal(QtFuture, tuple, dict, object)
cancelled = QtCore.Signal(str)
cancelled = QtCore.Signal(QtFuture)

def __init__(
self,
Expand All @@ -118,12 +120,11 @@ def __init__(
self.use_context = use_context

def on_called(self, future, args, kwargs, ctx):

try:
if self.use_context:
for ctx, value in ctx.items():
ctx.set(value)

if self.autoresolve:
x = self.coro(*args, **kwargs)
future.resolve(x)
Expand Down Expand Up @@ -151,35 +152,31 @@ async def acall(self, *args: P.args, timeout=None, **kwargs: P.kwargs):

except asyncio.CancelledError:
qtfuture._set_cancelled()
self.cancelled.emit(qtfuture)
raise


class QtListener():

def __init__(self,loop, queue) -> None:
class QtListener:
def __init__(self, loop, queue) -> None:
self.queue = queue
self.loop = loop

def __call__(self, *args):
self.loop.call_soon_threadsafe(self.queue.put_nowait, args)


class Iterator():

def __init__(self,queue, timeout=None) -> None:
class Iterator:
def __init__(self, queue, timeout=None) -> None:
self.queue = queue
self.timeout = timeout


def __anext__(self):
return self.next()


class QtSignal(QtCore.QObject, Generic[T, P]):

def __init__(
self,

signal: QtCore.Signal,
*args,
use_context=True,
Expand All @@ -196,7 +193,6 @@ def on_called(self, *returns):
for listener in self.listeners.values():
listener(*returns)


async def aiterate(self, timeout=None):
unique_id = uuid.uuid4().hex
loop = asyncio.get_event_loop()
Expand All @@ -217,14 +213,12 @@ async def aiterate(self, timeout=None):
except asyncio.CancelledError:
del self.listeners[unique_id]
raise

async def aonce(self, timeout=None):
async for i in self.aiterate(timeout=timeout):
return i




class QtRunner(KoilRunner, QtCore.QObject):
started = QtCore.Signal()
errored = QtCore.Signal(Exception)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "koil"
version = "0.2.12"
version = "0.2.14"
readme = "README.md"
description = "Async for a sync world"
authors = ["jhnnsrs <jhnnsrs@gmail.com>"]
Expand Down

0 comments on commit e8e429e

Please sign in to comment.