'How to save results inside a async event handler [Python, telethon]

I'm using a python script to listen to specific messages on telegram. Specifically, I'm using telethon and a regex. Now I want the client to be closed after the regex is matched. What I don't know is how to correctly store the found result. I got it running with using a global variable found, but I'm convinced this may lead to some undefined behavior in some situations because of the async nature. What would be the correct way to do this?

import re
from addresses import *
from telethon import TelegramClient, events

regex = "hello"
client = TelegramClient('session', TELE_ID, TELE_HASH)

global found

@client.on(events.NewMessage(chats=["test"]))
async def m_listener(event):
    found_regex = re.findall(regex, event.message.message, re.IGNORECASE)
    if len(found_regex)>0:      
        print("found....")
        global found
        found = found_regex[0]
        await client.disconnect()

with client:
    client.run_until_disconnected()
print(found)


Sources

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

Source: Stack Overflow

Solution Source