09-12-2024 09:51 AM
Dear all,
Make a sample python code with the function defined by async def inside a class.
I tried to execute it and get its return using LabVIEW but I got the error as below.
Do you have any solutions for this case?
I appreciate any helps from you.
Thanks.
import asyncio
class A:
async def example_coroutine(self):
print("This is a coroutine")
return 10
def test(self):
print("Test")
return 9
async def main():
await A.example_coroutine()
def create_class():
return A()
def wrap(obj):
return obj.example_coroutine()
09-12-2024 01:08 PM
@anhduy92 wrote:
Dear all,
Make a sample python code with the function defined by async def inside a class.
I tried to execute it and get its return using LabVIEW but I got the error as below.
Do you have any solutions for this case?
I appreciate any helps from you.
Thanks.
import asyncio class A: async def example_coroutine(self): print("This is a coroutine") return 10 def test(self): print("Test") return 9 async def main(): await A.example_coroutine() def create_class(): return A() def wrap(obj): return obj.example_coroutine()
What is the main() method supposed to do, its not called, maybe you mean to call that method?
Does this run in python?
09-12-2024 08:56 PM
asyncio.run(main())
I just removed the above execution line because I don't think it is useful for my check. So please ignore the main().
I'd like to execute the function example_coroutine defined in class A and get its return.
09-12-2024 09:15 PM
@anhduy92 wrote:asyncio.run(main())
I just removed the above execution line because I don't think it is useful for my check. So please ignore the main().
I'd like to execute the function example_coroutine defined in class A and get its return.
The way you call the wrap() method will only return a ref to the coroutine not start the async loop.
You need to start the async loop, once the async loop is running you can await methods. Check this out:
https://docs.python.org/3.9/library/asyncio-task.html#coroutine
09-12-2024 09:59 PM
Could you give me a quick solution for the wrap() so that I could call it in LabVIEW to get the return in "async def" function?
I am sorry, I am not familiar to Python much.
09-13-2024 09:51 AM
@anhduy92 wrote:
Could you give me a quick solution for the wrap() so that I could call it in LabVIEW to get the return in "async def" function?
I am sorry, I am not familiar to Python much.
Hmm, well this is not the best forum to lean Python, and you really need to lean some basic Python before diving into the asyncio library. I will post some Python below to get you started but I think you will find that the asyncio library and the LabVIEW Python VIs are somewhat at odds with each other when using the asyncio library. LabVIEW can execute VIs in parallel but calling the Python node in LV is blocking and there is some irony in using one parallel executing program to make a blocking call to an asynchronous library of another program. If you really want to link LabVIEW and Python asyncio you need to set up IPC between the two programs so the message loops in each program can run in parallel. Why are you trying to use asyncio, do you need it? Well here is some toy python asyncio code to play with:
import asyncio
import datetime
def log(msg:str):
print(f"{datetime.datetime.now()} || {msg}")
async def blocking_io():
# contrived IO blocking, IRL you would use await the read() but that requires code outside the
# standard Python lib
log('IO called')
buf = 0
for i in range(10):
# pretend to do some IO here
# fp = url.request.urlopen(r"http://www.python.org")
# buf = fp.read()
log(f"url request: {i}")
await asyncio.sleep(i*.2) # release the thread
log(f'IO blocking is done')
return buf
async def cpu_bound():
# CPU-bound operations will block the event loop:
# in general it is preferable to run them in a
# process pool. Ths is contrived to make it look like some process is doing lots of work.
log(f'sum called')
s = 0
for i in range(10):
s += i**i
log(f" running sum: {s}")
await asyncio.sleep(i/11) # release the thread
log(f'finished sum')
return s
def connection():
""" a dummy method """
log('connection called')
async def main():
log(f'Starting program')
try:
task1 = asyncio.create_task(blocking_io())
task2 = asyncio.create_task(cpu_bound())
data = await task1
connection() # note where this is called vs where it is executed
number = await task2
log(f'DATA: {data}')
log(f'NUMBER: {number}')
except Exception as ex:
log(f'Error {ex}')
finally:
log(f'Program is exit')
if __name__ == '__main__':
asyncio.run(main())