Card image cap

About asyncio

connection_made(self, transport)

  • called by asyncio when establishment a connection.

  • "Request to send" to disable.

  • Save transport object to instance variable and Make instance of SrialCommunication class.

data_received(self, data)

  • data_received is for data receive. data_received called by asyncio.

  • This method displays receive data. data reading paused.

connection_lost(self, exc)

  • if the connection is correctly closed, no exception will occur.

  • called by asyncio when connection lost. exc parameter is an exception object.

pause_writing(self)

  • if writing buffer is upper limit, called by asyncio.

resume_writing(self)

  • Called by asyncio when writing buffer is the acceptable range.

  • This method is used to resume writing.

pause_reading(self)

  • This method is used to pause data reading.

  • If data processing data takes a long time, or buffer overflow prevents reading, reading may be paused.

resume_reading(self)

  • This method resumes reading paused data.

イベントループの管理 Event loop management

asyncioのイベントループは非同期操作のスケジューリングと実行を管理し、適切なイベントループの管理は非同期プログラムのパフォーマンスと安定性に直接影響する。

バックプレッシャーのハンドリング Back pressure handling

pause_writingresume_writingメソッドは、書込みバッファのサイズに基づいてバックプレッシャーを管理するために使用される。これにより、メモリの過剰な使用を防ぎ、システムの安定性を保つことができる。

エラーハンドリング Error handling

connection_lostメソッドは接続が失われたときの処理を定義する。例外オブジェクトexcを適切に処理することでエラー発生時の適切なリソースの解放や再接続の試みなど堅牢なエラーハンドリングを実装できる。

リソースのクリーンアップ Resource cleanup

非同期プログラムでは、使用されなくなったリソースの適切な解放が特に重要。finallyブロックやconnection_lostメソッド内で解放すべきリソース(例えば、シリアルポートのクローズ)のクリーアップを忘れずに行う。

Category : Python, Posted : Feb. 11, 2024, 8:26 p.m.,
Update : Feb. 11, 2024, 10:28 p.m., By : koki,

Profile

Hello, my name is Koki. I am currently learning the web framework Django and English. In my free time, I enjoy hiking, cycling, and visiting shrines and temples. At work, I make software for programmable logic controllers (PLCs), I work especially with OMRON and Mitsubishi PLCs. Please feel free to contact me for any inquiries, job requests, or just to chat. We look forward to hearing from you via email!

Category

10 Latest articles