Search the Community
Showing results for tags 'asyncio'.
Found 2 results
-
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
-
Hi all, it is possible to use the asyncio with a DelphiVCL ui-based program? I've implemented an asyncua Client to get data from an OPC UA Server. Here is a simple program that prints some values collected from an OPC UA server. """CNC OPC UA Client.""" #------------------------------------------------------------------------------- # Name: cnc_opcua_client # # Purpose: CNC OPC UA Client # # Note Checked with Python 3.11.3 # # Coding Style https://www.python.org/dev/peps/pep-0008/ #------------------------------------------------------------------------------- import time import asyncio import logging from asyncua import Client # == opc ua client settings == OPC_UA_SERVER_URL = "opc.tcp://localhost:4840/" OPC_UA_SERVER_URI = "https://www.someone.com/opc-ua/server" OPC_UA_ASYNC_SLEEP_TIME = 0.01 # == deveopment settings == DEBUG_LEVEL = logging.CRITICAL # avoid any log not critical DEBUG_ENABLED = False # creates a logger logger = logging.getLogger(__name__) async def main(): """Main entry point.""" # creats opc ua client in with-exception-handled block async with Client(url=OPC_UA_SERVER_URL) as client: # gets namespace index idx = await client.get_namespace_index(OPC_UA_SERVER_URI) # gets program position node program_position_node = client.get_node("ns=2;s=CNC.ProgramPosition") # server updating loop while True: # does nothing, just process relax await asyncio.sleep(OPC_UA_ASYNC_SLEEP_TIME) # reads and prints program positions value = await program_position_node.read_value() print('X:{:10.3f}, Y:{:10.3f}, Z:{:10.3f}'. format(value[0], value[1], value[2])) # checks if this module is running as the main program if __name__ == "__main__": logging.basicConfig(level=DEBUG_LEVEL) asyncio.run(main(), debug=DEBUG_ENABLED) Getting: *** Remote Interpreter Reinitialized *** X: 51.206, Y: 0.000, Z: 0.000 X: 51.206, Y: 0.000, Z: 0.000 X: 51.206, Y: 0.000, Z: 0.000 X: 51.206, Y: 0.000, Z: 0.000 At this point I would like to create a fashioned UI using DelphiVCL, but I'm unable to understand where to place the asyncio.run(main(), debug=DEBUG_ENABLED) because to get DelphiVCL UI the main loop is already done by Application.Run() call: # initializes GUI Application Application.Initialize() Application.Title = "OPC UA Client Demo" # creates main application form app = DesktopView(Application) app.Show() FreeConsole() # enters in vcl main loop Application.Run() # frees main application form app.Destroy() Thanks in advance for any suggestion. Best Regards Silverio