In Python, how can I asynchronously await a FutureResult (returned from matlab.eng​ine.start_​matlab with background=True)?

In Python, I would like to be able to do something like this, within an async coroutine:
future_result = matlab.engine.start_matlab(background=True)
await future_result.wait()
As far as I can tell, the future_result.result() method will block until the result is ready, so that will not suffice. I could implement the async await with extra code like
await asyncio.get_running_loop().run_in_executor(
None, lambda: matlab.engine.start_matlab(background=True)
)
but it would be nice to have it built-in.

2 Comments

You can use the asyncio library’s wrap_future function to convert a concurrent.futures. Future object into an asyncio.Future object. This allows you to use the await keyword with it.
Thanks fot the suggestion, @Manikanta Aditya. Unfortunately, the matlab.engine.futureresult.FutureResult class returned by start_matlab does not appear to be related to the concurrent.futures.Future class required by wrap_future.
AssertionError: concurrent.futures.Future is expected, got <matlab.engine.futureresult.FutureResult object at 0x7fa65172b100>
(Pdb) result = matlab.engine.start_matlab(background=True)
(Pdb) result.__class__.mro()
[<class 'matlab.engine.futureresult.FutureResult'>, <class 'object'>]

Sign in to comment.

Answers (0)

Categories

Products

Release

R2023b

Asked:

on 22 Mar 2024

Edited:

on 1 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!