-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbasic_async.py
68 lines (52 loc) · 1.67 KB
/
basic_async.py
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
#
# This file is part of the linuxpy project
#
# Copyright (c) 2023 Tiago Coutinho
# Distributed under the GPLv3 license. See LICENSE for more info.
import argparse
import asyncio
import logging
import time
from linuxpy.video.device import Device
async def loop(variable):
while True:
await asyncio.sleep(0.1)
variable[0] += 1
async def run(device):
data = [0]
asyncio.create_task(loop(data))
with device:
start = last = time.monotonic()
last_update = 0
async for frame in device:
new = time.monotonic()
fps, last = 1 / (new - last), new
if new - last_update > 0.1:
elapsed = new - start
print(
f"frame {frame.frame_nb:04d} {len(frame)/1000:.1f} Kb at {fps:.1f} fps ; "
f" data={data[0]}; {elapsed=:.2f} s;",
end="\r",
)
last_update = new
def device_text(text):
try:
return Device.from_id(int(text))
except ValueError:
return Device(text)
def cli():
parser = argparse.ArgumentParser()
parser.add_argument("--log-level", choices=["debug", "info", "warning", "error"], default="info")
parser.add_argument("device", type=device_text)
return parser
def main(args=None):
parser = cli()
args = parser.parse_args(args=args)
fmt = "%(threadName)-10s %(asctime)-15s %(levelname)-5s %(name)s: %(message)s"
logging.basicConfig(level=args.log_level.upper(), format=fmt)
try:
asyncio.run(run(args.device))
except KeyboardInterrupt:
logging.info("Ctrl-C pressed. Bailing out")
if __name__ == "__main__":
main()