I use asyncio to run part of the lock code as follows:
result = await loop.run_in_executor(None, long_running_function)
My question is: can I set a timeout to execute long_running_function?
long_running_function
Basically, I don’t want it to long_running_functionlast more than 2 seconds, and I can’t do the correct processing of the timeout inside it, because this function comes from a third-party library.
You can use asyncio.wait_for :
future = loop.run_in_executor(None, long_running_function) result = await asyncio.wait_for(future, timeout, loop=loop)
Warning about canceling continuous functions:
Future, loop.run_in_executor asyncio.wait_for, long_running_function x , long_running_function, concurrent.futures, , , concurrent.futures.Future.
Future
loop.run_in_executor
asyncio.wait_for
x
concurrent.futures
concurrent.futures.Future
Source: https://habr.com/ru/post/1621616/More articles:Динамический порядок JPA с помощью API критериев - jpaText between RegEx Python pattern matching - pythonИзбавьтесь от лестницы if-else при создании запроса критериев JPA на основе полей sort/filter в LazyDataModel - javaInstagram API does not provide more than 100 followers of the user? - apiPassing Two BSTs in Order - algorithmAngular - umd.js vs rx.umd.js - arraysHow to display the data of another table as a grid in yii2? - phpIs adding and removing event listeners more efficient than not having them at all? - javaGenerating a tree of various integers results in a space leak - profilingJAXRS/Jersey 2 - ошибки проверки не вызывают ExceptionMapper - javaAll Articles