Repository: Avaiga/taipy
File: taipy/core/notification/core_event_consumer.py (Python)

Code before:
# specific language governing permissions and limitations under the License.

import abc
import threading
from queue import Empty, SimpleQueue

from .event import Event


class CoreEventConsumerBase(threading.Thread):
    """Abstract base class for implementing a Core event consumer.

    This class provides a framework for consuming events from a queue in a separate thread.
    It should be subclassed, and the `process_event` method should be implemented to define
    the custom logic for handling incoming events.

    Example usage:

    ```python
    class MyEventConsumer(CoreEventConsumerBase):
        def process_event(self, event: Event):
            # Custom event processing logic here
            print(f"Received event created at : {event.creation_date}")
            pass

    registration_id, registered_queue = Notifier.unregister(
        entity_type=EventEntityType.CYCLE,
        entity_id="CYCLE_cycle_1",
        operation=EventOperation.CREATION
    )

    consumer = MyEventConsumer("consumer_1", registered_queue)
    consumer.start()
    # ...
    consumer.stop()

    Notifier.unregister(registration_id)
    ```

    Subclasses should implement the `process_event` method to define their specific event handling behavior.

    Attributes:
        queue (SimpleQueue): The queue from which events will be consumed.


    """

    def __init__(self, registration_id: str, queue: SimpleQueue) -> None:
        """Initialize a CoreEventConsumerBase instance.

        Parameters:

Review comment (suggestion):
```suggestion
    registration_id, registered_queue = Notifier.register(
```

Code after:
# specific language governing permissions and limitations under the License.

import abc
import threading
from queue import Empty, SimpleQueue

from .event import Event


class CoreEventConsumerBase(threading.Thread):
    """Abstract base class for implementing a Core event consumer.

    This class provides a framework for consuming events from a queue in a separate thread.
    It should be subclassed, and the `process_event` method should be implemented to define
    the custom logic for handling incoming events.

    Subclasses should implement the `process_event` method to define their specific event handling behavior.

    Example usage:

    ```python
    class MyEventConsumer(CoreEventConsumerBase):
        def process_event(self, event: Event):
            # Custom event processing logic here
            print(f"Received event created at : {event.creation_date}")
            pass

    registration_id, registered_queue = Notifier.register(
        entity_type=EventEntityType.SCENARIO,
        operation=EventOperation.CREATION
    )

    consumer = MyEventConsumer(registration_id, registered_queue)
    consumer.start()
    # ...
    consumer.stop()

    Notifier.unregister(registration_id)
    ```

    Firstly, we would create a consumer class extending from CoreEventConsumerBase
    and decide how to process the incoming events by defining the process_event.
    Then, we would specify the type of event we want to receive by registering with the Notifier.
    After that, we create an object of the consumer class by providing
    the registration_id and registered_queue and start consuming the event.

    Attributes:
        queue (SimpleQueue): The queue from which events will be consumed.


    """