async def test_await_multiple_coroutines(): # ----------------------------------------- # wait for the completion of one coroutine # ----------------------------------------- label.text = "wait until button'A' is pressed or 5s passes" tasks = await ak.or_( ak.event(buttons[0], 'on_press'), ak.sleep(5), ) label.text = 'Done! ({})'.format( "button'A' was pressed" if tasks[0].done else "5s passed") await ak.sleep(1) label.text = 'next' await ak.sleep(1) # ------------------------------------------ # wait for the completion of all coroutines # ------------------------------------------ label.text = "wait until all buttons are pressed and 5s passes" tasks = await ak.and_( ak.sleep(5), *(ak.event(button, 'on_press') for button in buttons)) label.text = 'Done!' await ak.sleep(2)
def test_sleep(): import time from kivy.clock import Clock import asynckivy as ak Clock.tick() task = ak.start(ak.sleep(.1)) assert not task.done Clock.tick() assert not task.done time.sleep(.07) Clock.tick() assert not task.done time.sleep(.07) Clock.tick() assert task.done
async def rest_of_touch_moves(widget, touch, *, stop_dispatching=False, timeout=1.): ''' Returns an async-generator that iterates the number of times `on_touch_move` is fired, and ends when `on_touch_up` is fired. Grabs and ungrabs the touch automatically. If `stop_dispatching` is True, the touch will never be dispatched further i.e. the next widget will never get this touch until the generator ends. If `on_touch_up` was already fired, `MotionEventAlreadyEndedError` will be raised. ''' if touch.time_end != -1: # `on_touch_up` might have been already fired so we need to find out # it actually was or not. tasks = await or_( sleep(timeout), event(widget, 'on_touch_up', filter=lambda w, t: t is touch), ) if tasks[0].done: raise MotionEventAlreadyEndedError( f"MotionEvent(uid={touch.uid}) has already ended") else: return step_coro = await get_step_coro() on_touch_up, on_touch_move = _callbacks[not stop_dispatching] on_touch_up = partial(on_touch_up, step_coro, touch) on_touch_move = partial(on_touch_move, step_coro, touch) touch.grab(widget) uid_up = widget.fbind('on_touch_up', on_touch_up) uid_move = widget.fbind('on_touch_move', on_touch_move) assert uid_up assert uid_move # LOAD_FAST true_if_touch_move_false_if_touch_up = _true_if_touch_move_false_if_touch_up try: while await true_if_touch_move_false_if_touch_up(): yield touch finally: touch.ungrab(widget) widget.unbind_uid('on_touch_up', uid_up) widget.unbind_uid('on_touch_move', uid_move)
async def _see_if_a_touch_actually_is_a_dragging_gesture(self, touch): tasks = await ak.or_( ak.sleep(self.drag_timeout / 1000.), self._true_when_a_touch_ended_false_when_it_moved_too_much(touch), ) if tasks[0].done: # The given touch is a dragging gesture. if self._can_be_dragged: self._drag_task.cancel() self._drag_task = ak.Task( self._treat_a_touch_as_a_drag(touch, do_transform=True)) ak.start(self._drag_task) else: ak.start( self._simulate_a_normal_touch(touch, do_transform=True)) else: # The given touch is not a dragging gesture. ak.start( self._simulate_a_normal_touch(touch, do_touch_up=tasks[1].result))