Just checking if you found a way to run multi threaded with out blocking. we are trying to write a program which may have same challenges . we are hoping to create a python service running that connect to mutliple gcloud pubsub topic and write async call back function process the incoming messages in our existing universe subroutines.
Original Message:
Sent: 09-26-2024 02:33
From: Héctor Cortiguera
Subject: Multithreading in Python
Hi Mike
It's the second case, I'm using u2py because it's code intented to be run from U2. I don't want to do another login with uopy when I already have a working session in u2py.
As you can see in the example, I'm not using time.sleep, but the asyncio version. My problem is that the execution gets blocked for both asyncio threads while the u2py call is blocked.
I change the prints in the u2py coroutine
async def corutina_dormilona_universe():
i = 0
c = u2py.Command('ADORMIR')
while i < MAX_TIME/INTERVALO_LARGO:
print(f'{i} - {datetime.now()} - A DORMIR')
r = c.run()
print(f'{i} - {datetime.now()} - ME DESPIERTO')
i += 1
await asyncio.sleep(0)
return 'OK'
>PYTHON
python> from test_async import main_test
python> main_test()
-------------
* POSVENTA5 *
-------------
Hola hector (Pid: 2121618) Actualmente estas en la cuenta: POSVENTA5 Ruta: [/u2/quiter/POSVENTA5]
Hay 2 usuarios conectados de un total de 50
0 - 2024-09-26 08:29:42.083501 - HOLA
0 - 2024-09-26 08:29:42.083903 - A DORMIR
me despierto
0 - 2024-09-26 08:30:32.084532 - ME DESPIERTO
1 - 2024-09-26 08:30:32.084728 - A DORMIR
me despierto
1 - 2024-09-26 08:31:22.085280 - ME DESPIERTO
2 - 2024-09-26 08:31:22.085455 - A DORMIR
me despierto
2 - 2024-09-26 08:32:12.086017 - ME DESPIERTO
1 - 2024-09-26 08:32:12.086198 - HOLA
3 - 2024-09-26 08:32:12.086313 - A DORMIR
While u2py is running the command the second coroutine is not executing.
------------------------------
Héctor Cortiguera
Quiter Servicios Informaticos SL
Original Message:
Sent: 09-25-2024 10:16
From: Mike Rajkowski
Subject: Multithreading in Python
Hector,
Doing a sleep may not be the best test of how affective threading could be in Python.
see: https://stackoverflow.com/questions/92928/time-sleep-sleeps-thread-or-process#93179
Are you working on external Python code that works with u2py, or are you going into U2 first and calling Python.
If the external Python calling U2, I would suggest you look at uopy.
------------------------------
Mike Rajkowski
MultiValue Product Evangelist
Rocket Internal - All Brands
US
Original Message:
Sent: 09-25-2024 06:27
From: Héctor Cortiguera
Subject: Multithreading in Python
Hello all
Is there a way to run a slow u2py operation in background?
I ran some tests and the Universe Python seems to not support parallel execution due to GIL, so I discarded Python's native threads.
With async routines the execution gets blocked on u2py calls:
BASIC code:
>CT UP ADORMIR
ADORMIR
0001 SLEEP 50
0002 CRT 'me despierto'
0003 RETURN
0004
>
Python code:
import asyncio
import time
from datetime import datetime
import u2py
MAX_TIME = 60*2
INTERVALO_LARGO = 30
INTERVALO_CORTO = 1
async def corutina_rapida():
i = 0
while i < MAX_TIME/INTERVALO_CORTO:
print(f'{i} - {datetime.now()} - HOLA')
await asyncio.sleep(INTERVALO_CORTO)
i += 1
return 'OK'
async def corutina_dormilona_universe():
i = 0
c = u2py.Command('ADORMIR')
while i < MAX_TIME/INTERVALO_LARGO:
r = c.run()
print(f'{i} - {datetime.now()} - A DORMIR - {r}')
i += 1
await asyncio.sleep(0)
return 'OK'
async def lanza_corutinas():
lp = u2py.Command('LOGTO POSVENTA5').run()
await asyncio.gather(corutina_rapida(), corutina_dormilona_universe())
def main_test():
asyncio.run(lanza_corutinas())
When the u2py Command is running the other coroutine is blocked.
Is there a way to run this commands so that they don't block execution?
------------------------------
Héctor Cortiguera
Quiter Servicios Informaticos SL
------------------------------