|
9 | 9 | from dataclasses import dataclass |
10 | 10 | from contextlib import AbstractAsyncContextManager |
11 | 11 |
|
12 | | -from asyncgui import Cancelled, Task, move_on_when, _sleep_forever, _current_task |
| 12 | +from asyncgui import Task, move_on_when, _sleep_forever, _current_task |
13 | 13 |
|
14 | 14 | TimeUnit = TypeVar("TimeUnit") |
15 | 15 | ClockCallback: TypeAlias = Callable[[TimeUnit], None] |
@@ -383,28 +383,6 @@ async def interpolate_sequence(self, start, end, *, duration, step=0, transition |
383 | 383 | An alias for :meth:`interpolate_sequence`. |
384 | 384 | ''' |
385 | 385 |
|
386 | | - async def run_in_thread(self, func, *, daemon=None, polling_interval) -> Awaitable: |
387 | | - ''' |
388 | | - Creates a new thread, runs a function within it, then waits for the completion of that function. |
389 | | -
|
390 | | - .. code-block:: |
391 | | -
|
392 | | - return_value = await clock.run_in_thread(func, polling_interval=...) |
393 | | - ''' |
394 | | - raise NotImplementedError(r"'Clock.run_in_thread()' is not available because 'threading.Thread' is unavailable.") |
395 | | - |
396 | | - async def run_in_executor(self, executer, func, *, polling_interval) -> Awaitable: |
397 | | - ''' |
398 | | - Runs a function within a :class:`concurrent.futures.ThreadPoolExecutor`, and waits for the completion of the |
399 | | - function. |
400 | | -
|
401 | | - .. code-block:: |
402 | | -
|
403 | | - executor = ThreadPoolExecutor() |
404 | | - return_value = await clock.run_in_executor(executor, func, polling_interval=...) |
405 | | - ''' |
406 | | - raise NotImplementedError(r"'Clock.run_executor()' is not available because 'concurrent.futures.ThreadPoolExecutor' is unavailable.") |
407 | | - |
408 | 386 | def _update(setattr, zip, min, obj, duration, transition, output_seq_type, anim_params, task, p_time, dt): |
409 | 387 | time = p_time[0] + dt |
410 | 388 | p_time[0] = time |
@@ -509,69 +487,3 @@ def __aenter__(self, _current_task=_current_task) -> Awaitable[Callable[[], Awai |
509 | 487 |
|
510 | 488 | async def __aexit__(self, exc_type, exc_val, exc_tb): |
511 | 489 | self._event.cancel() |
512 | | - |
513 | | - |
514 | | -try: |
515 | | - from threading import Thread |
516 | | -except ImportError: |
517 | | - pass |
518 | | -else: |
519 | | - async def run_in_thread(clock: Clock, func, *, daemon=None, polling_interval) -> Awaitable: |
520 | | - return_value = None |
521 | | - exception = None |
522 | | - done = False |
523 | | - |
524 | | - def wrapper(): |
525 | | - nonlocal return_value, done, exception |
526 | | - try: |
527 | | - return_value = func() |
528 | | - except Exception as e: |
529 | | - exception = e |
530 | | - finally: |
531 | | - done = True |
532 | | - |
533 | | - Thread(target=wrapper, daemon=daemon, name="asyncgui_ext.clock.Clock.run_in_thread").start() |
534 | | - async with _repeat_sleeping(clock, polling_interval) as sleep: |
535 | | - while not done: |
536 | | - await sleep() |
537 | | - if exception is not None: |
538 | | - raise exception |
539 | | - return return_value |
540 | | - |
541 | | - run_in_thread.__doc__ = Clock.run_in_thread.__doc__ |
542 | | - Clock.run_in_thread = run_in_thread |
543 | | - |
544 | | - |
545 | | -try: |
546 | | - from concurrent.futures import ThreadPoolExecutor |
547 | | -except ImportError: |
548 | | - pass |
549 | | -else: |
550 | | - async def run_in_executor(clock: Clock, executer: ThreadPoolExecutor, func, *, polling_interval) -> Awaitable: |
551 | | - return_value = None |
552 | | - exception = None |
553 | | - done = False |
554 | | - |
555 | | - def wrapper(): |
556 | | - nonlocal return_value, done, exception |
557 | | - try: |
558 | | - return_value = func() |
559 | | - except Exception as e: |
560 | | - exception = e |
561 | | - finally: |
562 | | - done = True |
563 | | - |
564 | | - future = executer.submit(wrapper) |
565 | | - try: |
566 | | - async with _repeat_sleeping(clock, polling_interval) as sleep: |
567 | | - while not done: |
568 | | - await sleep() |
569 | | - except Cancelled: |
570 | | - future.cancel() |
571 | | - raise |
572 | | - if exception is not None: |
573 | | - raise exception |
574 | | - return return_value |
575 | | - |
576 | | - run_in_executor.__doc__ = Clock.run_in_executor.__doc__ |
577 | | - Clock.run_in_executor = run_in_executor |
0 commit comments