File: 15_async.py

package info (click to toggle)
python-trame 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (70 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import asyncio

from trame.app import asynchronous, get_server
from trame.ui.vuetify import SinglePageLayout
from trame.widgets import vuetify

# -----------------------------------------------------------------------------
# Trame app
# -----------------------------------------------------------------------------

server = get_server(client_type="vue2", ws_heart_beat=5)  # 5s
state, ctrl = server.state, server.controller

# -----------------------------------------------------------------------------
# State setup
# -----------------------------------------------------------------------------

state.count = 1
state.running_jobs = 0


@asynchronous.task  # <--- if commented the heartbeat will think the server is dead and crash server
async def start_count():
    with state:
        state.count = 0
        state.running_jobs += 1

    for i in range(int(state.delta)):
        with state:
            state.count += 1
            await asyncio.sleep(0.5)

    with state:
        state.running_jobs -= 1


ctrl.run = start_count


# -----------------------------------------------------------------------------
# UI setup
# -----------------------------------------------------------------------------
layout = SinglePageLayout(server)

with layout:
    with layout.toolbar as tb:
        vuetify.VSpacer()
        tb.add_child("RunningTasks({{ running_jobs }}) - CountSize({{ delta }})")
        vuetify.VSlider(
            v_model=("delta", 10),
            min=10,
            max=100,
            step=1,
            hide_details=True,
            dense=True,
            classes="mx-4",
        )
        with vuetify.VBtn(icon=True, click=ctrl.run):
            vuetify.VIcon("mdi-run")

    with layout.content:
        with vuetify.VContainer() as container:
            container.add_child("{{ count }}")

# -----------------------------------------------------------------------------
# start server
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    server.start()