mirror of
https://github.com/pyapp-kit/superqt.git
synced 2025-12-16 11:10:06 +01:00
fix nested calls (#63)
This commit is contained in:
@@ -247,8 +247,13 @@ class WorkerBase(QRunnable, Generic[_R]):
|
|||||||
|
|
||||||
self._worker_set.add(self)
|
self._worker_set.add(self)
|
||||||
self._finished.connect(self._set_discard)
|
self._finished.connect(self._set_discard)
|
||||||
start_ = partial(QThreadPool.globalInstance().start, self)
|
if QThread.currentThread().loopLevel():
|
||||||
QTimer.singleShot(10, start_)
|
# if we're in a thread with an eventloop, queue the worker to start
|
||||||
|
start_ = partial(QThreadPool.globalInstance().start, self)
|
||||||
|
QTimer.singleShot(1, start_)
|
||||||
|
else:
|
||||||
|
# otherwise start it immediately
|
||||||
|
QThreadPool.globalInstance().start(self)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _set_discard(cls, obj: WorkerBase) -> None:
|
def _set_discard(cls, obj: WorkerBase) -> None:
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import time
|
|||||||
import warnings
|
import warnings
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from operator import eq
|
from operator import eq
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -274,3 +275,20 @@ def test_abort_does_not_return(qtbot):
|
|||||||
assert loop_counter < 4
|
assert loop_counter < 4
|
||||||
assert abort_counter == 1
|
assert abort_counter == 1
|
||||||
assert return_counter == 0
|
assert return_counter == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_nested_threads_start(qtbot):
|
||||||
|
mock1 = Mock()
|
||||||
|
mock2 = Mock()
|
||||||
|
|
||||||
|
def call_mock():
|
||||||
|
mock1()
|
||||||
|
worker2 = qthreading.create_worker(mock2)
|
||||||
|
worker2.start()
|
||||||
|
|
||||||
|
worker = qthreading.create_worker(call_mock)
|
||||||
|
worker.start()
|
||||||
|
|
||||||
|
qtbot.wait(20)
|
||||||
|
mock1.assert_called_once()
|
||||||
|
mock2.assert_called_once()
|
||||||
|
|||||||
Reference in New Issue
Block a user