0

Suppose I have a Python iterator like this:

def my_iterator():
    data = heavy_computation()
    while data:
        yield data
        data = heavy_computation()
    
    
def client():
    it = my_iterator()
    for data in it:
        result = post_processing(data)
        print_to_file(result)

I want the iterator to start working on the next piece of data before the client asks for it. Is it possible to implement this type of behaviour using the python asyncio library, or any other more convenient method?

I'm new to the asyncio library, but it seems like the preferred way of handling concurrency in python. I'm open to other solutions as well.

akvilas
  • 586
  • 2
  • 5
  • 15
  • See https://stackoverflow.com/questions/61351844/difference-between-multiprocessing-asyncio-threading-and-concurrency-futures-i and https://stackoverflow.com/questions/27435284/multiprocessing-vs-multithreading-vs-asyncio. Your problem suggests an asynchronous method of calculation (multiprocessing, perhaps) that writes its results to a queue of some sort. The client removes items from the queue for post processing and printing. – Paul Cornelius Dec 05 '22 at 18:14

0 Answers0