CBAPKA 0 Posted March 14 (edited) Is there any way I can run the script without the main thread hanging? I can run the code in a stream, but I don't understand how it works to the end, because I can execute the code in PyCharm and everything works fine. For example, I initialize the stream directly inside the python script, put the code into it, but the Library telethon that performs the task throws the error "There is no current event loop" или "There is no current event loop in thread 'Thread-1 (main)'." или "There is no current event loop in thread 'Dummy-1'." In pycharm that work as def CreateConnect(path: str, proxy, api_hash, api_id, device=None, system=None, loop=None): TC = TelegramClient( api_hash=api_hash, api_id=api_id, session=path, timeout=60, device_model=device, system_version=system, proxy=proxy, loop=loop ) try: TC.connect() if TC.is_connected(): return TC else: return False except Exception as e: print(f'error {path}: {e}') return False def main(loop): asyncio.set_event_loop(loop) #... tg = CreateConnect(path=f'{accPath}\\{account}\\telethon.session', proxy=proxy, api_hash=settings['telethon']['api_hash'], api_id=settings['telethon']['api_id'], device=settings['telethon']['device_model'], system=settings['telethon']['system_version'], loop=loop ) if __name__ == '__main__': loop = asyncio.new_event_loop() p = threading.Thread(target=main, args=(loop,)) p.start() but in delphi, the Thread is initialized on top of the module, as I understood it Edited March 14 by CBAPKA Share this post Link to post
pyscripter 694 Posted March 14 2 hours ago, CBAPKA said: Is there any way I can run the script without the main thread hanging? See PythonThreads · pyscripter/python4delphi Wiki (github.com) which explains how to run python code in delphi threads. This keeps the delphi main thread from blocking. Share this post Link to post
CBAPKA 0 Posted March 14 1 hour ago, pyscripter said: which explains how to run python code in delphi threads. This keeps the delphi main thread from blocking. I read it, but my problem in this: GetPythonEngine.ExecStrings(Lines); Lines: import asyncio loop = asyncio.new_event_loop () and got error: ValueError: set_wakeup_fd only works in main thread of the main interpreter my task is just to run asynchronous code in python, without hanging the delphi interface itself, but it turns out that pythonThread From delphi Wraps the script into a thread and I do not know how to get the Loop of the Main thread Share this post Link to post
pyscripter 694 Posted March 14 (edited) Assuming you are on Windows use: import asyncio asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) loop = asyncio.new_event_loop() This should work both in the main thread and in other threads. Google for "set_wakeup_fd" to see this is a well known issue. Edited March 14 by pyscripter Share this post Link to post
CBAPKA 0 Posted March 14 1 hour ago, pyscripter said: Assuming you are on Windows use: Thank you very much. You have solved my problem. Share this post Link to post