#🔒 How to make the atexit function work when a while loop is in progress

5 messages · Page 1 of 1 (latest)

bleak reef
#

Hi, basically it's a little python tool that retrieves data from a game and sends it to a google sheet every second, except that I'd like it to send a last request in disconnection mode when you close the tool.

So I've used an atexit but it doesn't work when I close my tool. It works when you press ctrl+c but not when you close the tool with the cross.

dim voidBOT
#

@bleak reef

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

bleak reef
#
def envoyer_derniere_requete():
    try:
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)
        service = build("sheets", "v4", credentials=creds)
        sheet = service.spreadsheets()

        export = NOM + "&" + "V1.0" + "&" + "déconnecté" 

        sheet.values().update(
            spreadsheetId=spreadheets_ID,
            range=f"ETTS!A{LIGNE}",
            valueInputOption="USER_ENTERED",
            body={"values": [[export]]}
        ).execute()
    except Exception as e:
        print("Erreur lors de l'envoi de la dernière requête :", e)
        raise e


if __name__ == "__main__":
    print("Démarrage de l'ETTS, vérification du token et de l'allumage du jeu")
    atexit.register(envoyer_derniere_requete)
    main()
dim voidBOT
#

@bleak reef

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.