'INSERT/UPDATE based on signal | Python, SQL Server

What is the best way to process a two-state signal into individual events and dump this into a database using a python script? Are there maybe some dedicated libraries for this?

A more precisely described case:

I have N-number of button signals (value 0 or 1) and I would like when a value 1 occurs, to add information about it to the database and when this signal persists over time to update this record with the final time until the signal 0 occurs. It is important that the record is updated in the database not less frequently than every 1 second.

Below the diagram:

Diagram

Thank you for all the answers in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source