Ok, lets see if we can make this script a little better. To do that we are going to exploit the use of Co-routines using the Python 3 library async and await . The allows the code to start all the work it can at whatever time it wants to, and then as each "awaited" task pauses for i/o to let another task have control of the process. We've already split the work into as many processes as we have on our host computer, now we will keep all of them busy as possible. I'm using the asyncio.wait(...) function because we probably want to run this as a lambda and we'll want to stop the work before our allotted time runs out and report the results we do have, as well as report that we ran out of time. So that our future selves can either increase the max allotted time, or report it as an error. ''' Sample code to show how to use Multiprocessing and Co-routines. ''' import time from multiprocessing import Process, Pipe, cpu_count import asyncio ...
Posts
Showing posts from June, 2019