Note that there are some explanatory texts on larger screens.

plurals
  1. POQueueing work, without using a lot of memory
    primarykey
    data
    text
    <p>I'm trying to set something up where one thread is writing a list of work and another thread is reading the list and working from it. This list can be very large so to stop this list being held in memory I want to have it written in a file (or anyway of preserving memory- generators?).</p> <p>I put together a little runnable example with a sleep in the writer so that the reader can catch up. I'm wondering how I can get the reader to not stop when it "overtakes" the writer. I looked at using <code>.seek</code> and <code>.tell</code> but I got weird behaviour and I'm not sure that's the right route.</p> <p>As another question, is this at all a sensible idea? Maybe there's a much more elegant way I can queue up a list of strings without using loads of memory.</p> <pre><code>import threading,time class Writer(threading.Thread): lock= threading.Lock() def __init__(self,file_path,size): threading.Thread.__init__(self) self.file_path= file_path self.size= size self.i=0 def how_many(self): with self.lock: print "Reader starting, writer is on",self.i def run(self): f=open(self.file_path,"w") for i in xrange(self.size): with self.lock: self.i=i if i%1000==0: time.sleep(0.1) f.write("%s\n"%i) f.close() class Reader(threading.Thread): def __init__(self,file_path): threading.Thread.__init__(self) self.file_path= file_path def run(self): f=open(self.file_path,"r") line=0 for line in f: pass print "Reader got to: %s"%line.strip() if __name__ == "__main__": a= Writer("testfile",2000000) b= Reader("testfile") a.start() time.sleep(1) a.how_many() b.start() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload