# Clock
*An event scheduler designed for asyncgui programs.*
First, take a look at the callback-style code below that has nothing to do with `asyncgui`.
If you've ever used `Kivy` or `Pyglet`, you may find it familiar.
```python
from asyncgui_ext.clock import Clock
clock = Clock()
# Schedules a function to be called after a delay of 20 time units.
clock.schedule_once(lambda dt: print("Hello"), 20)
# Advances the clock by 10 time units.
clock.tick(10)
# The clock advanced by a total of 20 time units.
# The callback function will be called.
clock.tick(10) # => Hello
```
Next one is async/await-style code that involves `asyncgui`, and does the same thing as the previous.
```python
import asyncgui
from asyncgui_ext.clock import Clock, sleep
clock = Clock()
async def async_fn():
await sleep(clock, 20)
print("Hello")
asyncgui.start(async_fn())
clock.tick(10)
clock.tick(10) # => Hello
```
These two examples effectively illustrate how this module works but they are not practical.
In a real-world program, you probably want to call ``clock.tick()`` in a loop or schedule it to be called repeatedly using another scheduling API.
For example, if you are using `PyGame`, you may want to do:
```python
clock = pygame.time.Clock()
vclock = asyncgui_ext.clock.Clock()
# main loop
while running:
...
dt = clock.tick(fps)
vclock.tick(dt)
```
And if you are using `Kivy`, you may want to do:
```python
from kivy.clock import Clock
vclock = asyncui_ext.clock.Clock()
Clock.schedule_interval(vclock.tick, 0)
```
## Installation
Pin the minor version.
```
poetry add asyncgui-ext-clock@~0.2
pip install "asyncgui-ext-clock>=0.2,<0.3"
```
## Tested on
- CPython 3.10
- CPython 3.11
- CPython 3.12
## Misc
- [YouTube Demo](https://youtu.be/kPVzO8fF0yg) (with Kivy)
Raw data
{
"_id": null,
"home_page": "https://github.com/asyncgui/asyncgui-ext-clock",
"name": "asyncgui-ext-clock",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "async",
"author": "Natt\u014dsai Mit\u014d",
"author_email": "flow4re2c@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/65/ab/23eb260ffb58ddffe0ffd53f713cb8679c44030baa82c19930b1d912d88d/asyncgui_ext_clock-0.2.0.tar.gz",
"platform": null,
"description": "# Clock\n\n*An event scheduler designed for asyncgui programs.*\n\nFirst, take a look at the callback-style code below that has nothing to do with `asyncgui`.\nIf you've ever used `Kivy` or `Pyglet`, you may find it familiar.\n\n```python\nfrom asyncgui_ext.clock import Clock\n\nclock = Clock()\n\n# Schedules a function to be called after a delay of 20 time units.\nclock.schedule_once(lambda dt: print(\"Hello\"), 20)\n\n# Advances the clock by 10 time units.\nclock.tick(10)\n\n# The clock advanced by a total of 20 time units.\n# The callback function will be called.\nclock.tick(10) # => Hello\n```\n\nNext one is async/await-style code that involves `asyncgui`, and does the same thing as the previous.\n\n```python\nimport asyncgui\nfrom asyncgui_ext.clock import Clock, sleep\n\nclock = Clock()\n\nasync def async_fn():\n await sleep(clock, 20)\n print(\"Hello\")\n\nasyncgui.start(async_fn())\nclock.tick(10)\nclock.tick(10) # => Hello\n```\n\nThese two examples effectively illustrate how this module works but they are not practical.\nIn a real-world program, you probably want to call ``clock.tick()`` in a loop or schedule it to be called repeatedly using another scheduling API.\nFor example, if you are using `PyGame`, you may want to do:\n\n```python\nclock = pygame.time.Clock()\nvclock = asyncgui_ext.clock.Clock()\n\n# main loop\nwhile running:\n ...\n\n dt = clock.tick(fps)\n vclock.tick(dt)\n```\n\nAnd if you are using `Kivy`, you may want to do:\n\n```python\nfrom kivy.clock import Clock\n\nvclock = asyncui_ext.clock.Clock()\nClock.schedule_interval(vclock.tick, 0)\n```\n\n## Installation\n\nPin the minor version.\n\n```\npoetry add asyncgui-ext-clock@~0.2\npip install \"asyncgui-ext-clock>=0.2,<0.3\"\n```\n\n## Tested on\n\n- CPython 3.10\n- CPython 3.11\n- CPython 3.12\n\n## Misc\n\n- [YouTube Demo](https://youtu.be/kPVzO8fF0yg) (with Kivy)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": null,
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/asyncgui/asyncgui-ext-clock",
"Repository": "https://github.com/asyncgui/asyncgui-ext-clock"
},
"split_keywords": [
"async"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6d972a1929c477f611a8ed148803c3f36505d0df800fa26c259306a86ba47eba",
"md5": "b02b39a45673d95cb11e321ba9c4528f",
"sha256": "4d05665dc12b293d3ab54512231ee904b6dc312873cd52fd7c7f5f5ea6d223e3"
},
"downloads": -1,
"filename": "asyncgui_ext_clock-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b02b39a45673d95cb11e321ba9c4528f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 7132,
"upload_time": "2024-05-03T11:35:23",
"upload_time_iso_8601": "2024-05-03T11:35:23.478107Z",
"url": "https://files.pythonhosted.org/packages/6d/97/2a1929c477f611a8ed148803c3f36505d0df800fa26c259306a86ba47eba/asyncgui_ext_clock-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "65ab23eb260ffb58ddffe0ffd53f713cb8679c44030baa82c19930b1d912d88d",
"md5": "087aba7c39313557a6e0f928ce734bc7",
"sha256": "54899223d6d7d5ab528c599c9a8fd982b3a1762f67f0d043290a55ef56d259f9"
},
"downloads": -1,
"filename": "asyncgui_ext_clock-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "087aba7c39313557a6e0f928ce734bc7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 6809,
"upload_time": "2024-05-03T11:35:25",
"upload_time_iso_8601": "2024-05-03T11:35:25.267871Z",
"url": "https://files.pythonhosted.org/packages/65/ab/23eb260ffb58ddffe0ffd53f713cb8679c44030baa82c19930b1d912d88d/asyncgui_ext_clock-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-03 11:35:25",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "asyncgui",
"github_project": "asyncgui-ext-clock",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "asyncgui-ext-clock"
}