异步的lirc python扩展

aiolirc的Python项目详细描述


http://img.shields.io/pypi/v/aiolirc.svghttps://img.shields.io/badge/license-GPLv3-brightgreen.svg

关于

使用python的新工具(async await语法)的异步消息传递,在3.5版中引入是如此有趣!

所以,我决定为 Linux Infra-Red Remote Control(LIRC)

幸运的是,cython在asyncio上运行良好。因此,lirc_clientc扩展是由cython扩展而来的 键入。

此外,还提供了ircspatcher类型和listen_for装饰器。

安装

$ apt-get install liblircclient-dev python3.5-dev build-essential
$ pip install cython
$ pip install aiolirc

快速启动

使用此库的最简单方法是著名的快速启动功能,如下所示:

from aiolirc import very_quickstart, listen_for

@listen_for('play')
async def do_play(loop):
    ...
    # Do play stuff

very_quickstart('my-prog')  # my-prog is configured in your lircrc file.

另一个名为快速启动的协同程序函数也可用。 生命周期:

import asyncio
from aiolirc import quickstart

main_loop = asyncio.get_event_loop()
try:
    main_loop.run_until_complete(quickstart(loop=main_loop))
except KeyboardInterrupt:
    print('CTRL+C detected. terminating...')
    return 1
finally:
    if not main_loop.is_closed():
        main_loop.close()

irc调度器

构造器

def __init__(self, source: LIRCClient, loop: asyncio.BaseEventLoop=None):

用法示例

import asyncio

from aiolirc.lirc_client import LIRCClient
from aiolirc.dispatcher import IRCDispatcher, listen_for

@listen_for('amp power', repeat=5)
async def amp_power(loop):
    ...
    # Do your stuff

@listen_for('amp source')
async def amp_source(loop):
    ...
    # Do your stuff


async with LIRCClient('my-prog') as client:
    dispatcher = IRCDispatcher(client)
    await dispatcher.listen()

lircclient网站

构造函数

def __cinit__(self, lircrc_prog, *, lircrc_file='~/.config/lircrc', loop=None, check_interval=.05, verbose=False,
    blocking=False):

为了进一步控制从lirc接收到的消息,在 调用lircclient.lirc_init()。并确保在完成工作后调用了lircclient.lirc_deinit()。 使用lircclient

from aiolirc.lirc_client import LIRCClient

client = LIRCClient('my-prog')
try:
    client.lirc_init()
    async for cmd in client:
        print(cmd)
finally:
    client.lirc_deinit()

您可以使用lircclient作为异步上下文管理器,如下所述,以自动调用 lircclient.lirc_init()lircclient.lirc_deinit()函数,并获取一个锁以防止 从lirc_client wrapper读取消息中的lircclient实例:

from aiolirc.lirc_client import LIRCClient

async with LIRCClient('my-prog') as client:
    async for cmd in client:
        print(cmd)

系统d

创建main.py:

import sys
import asyncio

from aiolirc import IRCDispatcher, LIRCClient

async def launch(self) -> int:

    async with LIRCClient('my-prog', lircrc_file='path/to/lircrc', check_interval=.06) as client:
        dispatcher = IRCDispatcher(client)
        result = (await asyncio.gather(dispatcher.listen(), return_exceptions=True))[0]

    if isinstance(result, Exception):
        raise result

    return 0

def main(self):

    main_loop = asyncio.get_event_loop()
    try:
        return main_loop.run_until_complete(launch())
    except KeyboardInterrupt:
        print('CTRL+C detected.')
        return 1
    finally:
        if not main_loop.is_closed():
            main_loop.close()

if __name__ == '__main__':
    sys.exit(main())

/etc/systemd/system/aiolirc.service文件:

[Unit]
Description=aiolirc

[Service]
ExecStart=python3.5 /path/to/main.py
User=user
Group=group

[Install]
WantedBy=multi-user.target

系统控制:

$ systemctl enable aiolirc
$ systemctl start aiolirc
$ systemctl restart aiolirc

$ ps -Af | grep 'main.py'

$ systemctl stop aiolirc

更改日志

0.1.0

  • README.rst

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在通过REST Api发送之前,java是否会操纵map中键的顺序?   java实时代码编译?   java在尝试使用JavaFX制作东西时获得0xC0000409   java截图并返回servlet?   java哪个用户认证集成安全性?   SpringJavaMailSender和MS Exchange:java。lang.noclassdeffounderror:com。太阳邮政uti。邮件记录器   Linux上的Java小程序无法获得权限   java如何获得一行(范围)中字符串的组合?   媒体播放器的java意图过滤器(Youtube视频播放)   java如何明确地确定文件是否不是我想要的类型?   Java 8中并发异步方法后跟并行执行方法