Issue
https://github.com/jborean93/smbprotocol
We use the library above in our project, but we want to accelerate the API by either using thread pools or asynchronous programming which is provided by asyncio library. We wonder if we can use this library with the operations in which I/O bound operations take place. If it can be achieved, could you give some examples?
Solution
Yes. The idea is to run all legacy blocking IO calls in executors - the asyncio library provides the run_in_executor call, which takes care of that.
Just, since it is I/O bound, provide a custom executor instead of the default one created by asyncio (when you pass "None" as the executor): the default is conservative, as the task to be executed might be CPU bound - so it have a number of workers proportional to the available CPU cores. For an I/O bound task, you might get good results around 15 workers for an average network link, and much more if you have bandwidth of the order of 1GBpS.
The usage could not be simpler: just write your app with co-routine functions made to be run under the asyncio loop, and call the legacy lib with
asyncio.create_task(asyncio.run_in_executor(executor, legacy_function, *args_to_legacy_function))
Answered By - jsbueno
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.