Skip to content

Conversation

@glesperance
Copy link
Contributor

@glesperance glesperance commented Nov 22, 2024

Force a flush from the internal queue to the server at exit.

This is necessary for properly sending events to langfuse when requests are happening in ephemeral / distributed processes.


Important

Replaces atexit.register(self.join) with atexit.register(self.shutdown) in TaskManager to ensure queue flush at exit.

  • Behavior:
    • Replaces atexit.register(self.join) with atexit.register(self.shutdown) in TaskManager.__init__() to ensure a flush of the internal queue to the server at exit.
  • Functions:
    • shutdown() now handles both flushing and joining of consumer threads, ensuring clean shutdown.

This description was created by Ellipsis for 22ec717. It will automatically update as commits are pushed.

@CLAassistant
Copy link

CLAassistant commented Nov 22, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: Experimental PR review

PR Summary

This PR modifies the TaskManager to ensure proper event flushing before application shutdown, particularly important for ephemeral and distributed processes.

  • Added shutdown method registration with atexit in TaskManager to force flush queued events before exit
  • Modified cleanup sequence to flush pending events before joining threads in /langfuse/_task_manager/task_manager.py
  • Added example shutdown handlers in /examples/django_example/myapp/__init__.py and FastAPI example for proper integration
  • Ensures data integrity by preventing event loss in short-lived processes

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: Experimental PR review

PR Summary

This PR adds comprehensive examples demonstrating proper shutdown handling in Django and FastAPI applications to ensure all Langfuse events are flushed before application termination.

  • Added signal handlers (SIGINT/SIGTERM) in /examples/django_example/myapp/__init__.py to properly flush events
  • Implemented FastAPI lifespan context manager in /examples/fastapi_example/fastapi_example/main.py for graceful shutdown
  • Added detailed README documentation for both Django and FastAPI examples explaining shutdown behavior

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@hassiebp hassiebp merged commit dd25492 into langfuse:main Nov 26, 2024
1 check passed
@hassiebp
Copy link
Contributor

Thanks a lot for your contribution @glesperance !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(sdk-python): sdk should force a flush from the internal queue to the server at exit

3 participants