I'm trying to upload a file in thread. So later I can add callback with progress bar update etc.
I've tried to do that with threading and PySimpleGui's perform_long_operation
.
It is not working this way.
So how do I do it? Probably, I'm missing something about asyncio usage.
from pyrogram import Client
from time import sleep
import PySimpleGUI as sg
import asyncio
import random
import sys
api_id = 234242434
api_hash = '324423423'
sg.change_look_and_feel('DarkAmber')
layout = [
[sg.Text('Password'), sg.InputText(password_char='*', key='password')],
[sg.Text('', key='status', size=(20, 1))],
[sg.Button('Submit'), sg.Button('Cancel')]
]
window = sg.Window('TG Send', layout, finalize=True)
client = None
def long_proc():
client = Client('tg_send', api_id, api_hash)
window['status'].update('connecting')
client.connect()
window['status'].update('started')
done = False
while True:
if not done:
print('Uploading file')
window['status'].update('uploading')
client.send_document('me', document='File.db')
done = True
print('Uploading done')
window['status'].update('uploaded')
else:
sleep(1)
async def ui():
while True:
event, value = window.read(timeout=1)
if event in (None, 'Cancel'):
sys.exit()
if event != '__TIMEOUT__':
print(f"{event=} {value=}")
if event == 'Submit':
window.perform_long_operation(long_proc, '-FUNCTION COMPLETED-')
await asyncio.sleep(0)
async def main():
await asyncio.gather(
asyncio.create_task(ui()),
# asyncio.to_thread(long_proc),
)
if __name__ == '__main__':
asyncio.run(main())