Skip to content

fix: Add a optional grace time period to shutdown method #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

bohan-amplitude
Copy link
Contributor

Summary

Add optional parameter: grace_time_milliseconds to client.shutdown()
Allow user to delay main interpreter thread close waiting the last batch events task scheduled to threads pool.

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: no

"""Shutdown the client instance, not accepting new events, flush all events in buffer"""
self.configuration.opt_out = True
self.__timeline.shutdown()
if grace_time_milliseconds > 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the quick update! Not sure whether we should add this here, as if shutdown is exposed as public API, user can do the time.sleep if they need. Do we consider to use async tasks for the worker? https://docs.python.org/3/library/asyncio-task.html.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current worker use threading. Switch to coroutine may need some work to re-design and implement the retry logic. shutdown() is a public interface suppose to be called before application exit. I have run some tests. Add ether flush or shutdown after tracking will stop the error of can not schedule future. Let me ask customer if they added these call.

from amplitude import *


client = Amplitude('')

client.track(BaseEvent(
    event_type='Python Event',
    user_id='[email protected]',
    ip='127.0.0.1',
    event_properties={
        'keyString': 'valueString',
        'keyInt': 11,
        'keyBool': True
    }
))

# client.flush()
client.shutdown()```

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I thought this fix is for the other issue (timing issue). I cannot reproduce the error can not schedule future in both Python 3.9 and 3.10. If adding this can fix the issue, I am good with change. Just to double check, will this be stable with the usage of sleep?

Copy link

Choose a reason for hiding this comment

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

@bohan-amplitude Can you please first enable py37-p311 pipelines, so we can see what really happens with different python versions, running only on py36 is insane, that version pinned for the fjords long time ago.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @ssbarnea, just shared open another pull request with workaround for this issue at #43, feel free to take a look and let us know your thoughts. Seems that passes the test on py3.6 to py3.11.

@ssbarnea
Copy link

ssbarnea commented Jan 8, 2023

I added #41 but apparently somene is really afraid about incomming pull requests... to the extent of requiring approval ;)

@qingzhuozhen
Copy link
Contributor

Closing this one for now since we pushed a fix in v1.1.1. Feel free to try the new version and let us know if the experience is improved or not. Thanks.

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.

4 participants