Skip to main content

Ex-Machina

python で bot 書くためのフレームワーク

Test Coverage Package version Supported Python versions


インストール

using pip

pip install -U exmachina

using poetry

poetry add exmachina@latest

使い方

import asyncio

from exmachina import Event, Machina, Retry, RetryFixed

bot = Machina()

bot.create_concurrent_group(
    name='limit',
    entire_calls_limit=4,
    time_calls_limit=3,
    time_limit=1,
)

@bot.emit(interval='1s')
async def emit(event: Event):
    res = await event.execute('execute')
    assert res == 42
    # or
    res = await execute()
    assert res == 42

# 特定の例外でリトライしたい場合
retry = Retry([
  RetryFixed(HTTPError, wait_time=1, retries=5),
])

@bot.execute(concurrent_groups=['limit'], retry=retry)
async def execute():
    return 42

if __name__ == "__main__":
    try:
        wasyncio.run(bot.run())
    except KeyboardInterrupt:
        print("終了")

Emit

定期的に実行したい関数を登録するためのデコレータ

  • name
    • Emitでユニークな名前をつける
    • 省略した場合はデコレートした関数名になる
  • count
    • 実行回数. これで指定した回数実行した後、aliveがFalseになる
    • Noneを指定した場合は無限回実行する
    • デフォルトはNone
  • interval
    • ループのインターバル 1sや1d4hなどと指定できる
    • デフォルトは0s. つまり待機しない
  • alive
    • Trueの場合、botの実行時に自動で実行される
    • 手動で起動する場合はFalseを指定する
    • デフォルトはTrue

Concurrent Group

時間あたりや同時実行数を制限するグループ
作成したグループは後述のExecuteに対して設定できる

  • name
    • 必須プロパティ. Concurrent Groupでユニークな名前をつける
  • entire_calls_limit
    • 全体の実行数制限
    • このグループに所属するExecuteの並列実行数
    • Noneを指定した場合、無制限
    • デフォルトはNone
  • time_calls_limit
    • このグループに所属するtime_limit秒あたりに実行"開始"できるExecuteの数
    • デフォルトは1
  • time_limit
    • time_calls_limitの制限時間(秒)
    • デフォルト0. つまり、制限なし

Execute

Emitから呼び出される、一回きりのタスク
Emitは主にbotの制御を行い、Executeは計算処理や外部との通信を行う処理を書く想定

  • name
    • Executeでユニークな名前をつける
    • 省略した場合はデコレートした関数名になる
  • concurrent_groups
    • executeの所属するconcurrent_groupを配列で指定する
    • デフォルトは[]
  • retry
    • Execute実行中に発生した例外をトリガーにリトライを行う設定を指定する
    • from exmachina import Retry
    • デフォルトはNone

Event

Emitする関数に渡されるオブジェクト

  • 停止中の別のEmit起動や停止
  • Executeの実行
  • ループの状態を確認できる属性を持つ

Emitの起動と停止

event.start('emit_name')
event.stop('emit_name')

Executeの実行

event.execute('execute_name', *args, **kwargs)

または、直接呼び出し

await execute_name(*args, **kwargs)

event.executeはexecuteのTaskを返す

属性

event.epoch # emitのループ回数
event.previous_execution_time # 直前のループの処理時間
event.count # emitの残りの実行回数(未指定の場合はNone)

Retry

Executeのリトライの設定を書くためのもの
特定の秒数を待機した後、そのまま再実行を行う

  • rules
    • 指定するルールの配列
  • logger
    • 自前のloggerを渡したい場合に使う

RetryRule

  • RetryFixed: 常にwait_timeの間隔でリトライ
  • RetryFibonacci: 1,1,2,3,5,...とフィボナッチ数列秒の間隔でリトライ
  • RetryRange: minとmaxを指定し、その間の乱数秒の間隔でリトライ
  • RetryExponentialAndJitter: 指数関数倍的に最大の待機時間を伸ばしつつリトライ のいずれかを使用する

共通引数

  • exception
    • 指定する例外のクラス
  • retries
    • リトライ回数. 省略するとRecursionErrorが出るまで再実行する
    • デフォルトはNone
  • filter
    • 例外インスタンスを引数にboolを返す関数を指定
    • Trueを返した場合にこのリトライ条件にマッチする
    • HTTPのステータスコードなどで引っ掛けたいリトライ設定が異なる場合などを想定
    • デフォルトはNone. つまり、常にTrueを返す

開発

init

poetry install
poetry shell

fmt

poe fmt

lint

poe lint

test

poe test

Metadata

Release files for exmachina 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for exmachina 0.2.4
File Size Uploaded Provenance
exmachina-0.2.4.tar.gz 14.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for exmachina 0.2.4
File Interpreter ABI Platform
exmachina-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 29.3 kB

Release files / exmachina-0.2.4.tar.gz

Download URL exmachina-0.2.4.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a3af6abcdc4778b702ceb1b9bcc021ae7669df0c8764eef48c180712bad9daaf
BLAKE2b-256 checksum
How to use checksums
9ae0bac6e0ee6dcffce7dfb1a27aba53fdcc1c01f8745e5751610f8baaa367ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.5 Linux/5.13.0-1031-azure

Release files / exmachina-0.2.4-py3-none-any.whl

Download URL exmachina-0.2.4-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6affecb057d1c2816a0dc8d64048639331a16319762fd0f3566236e3a8b64999
BLAKE2b-256 checksum
How to use checksums
10372ac4b99c844b4ed5d13c0a230322bc7b970ee17b6e0d0d7bd2c171c05635
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.5 Linux/5.13.0-1031-azure

Security for exmachina 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page