multithreading - Write with several threads to a list and read it with an other - Python -


hi got problem multithreading. i'm trying getting data http requests. want data 3 webservers. i'm using threading module in python.

what want write data array , sort it.

thread0 writes data array[0] thread1 writes data array[1] thread2 writes data array[2] 

this happens when data fetched webserver has changed. thread4 should copy array when has changed , sort , processing on copy afterwards.

this plan. how can without blocking other thread's write actions , how can assure data not inconsistant when read it.

here how far have gotten idea.

here module 1 thread:

import threading  class http_fetcher(threading.thread):      _idx_of_list = []       def set_idx(self,idx)          self._idx_of_list = idx       def run(self)          global my_list           #some http fetching stuff           old_data = []           if old_data != actual_data:              old_data = actual_data              my_list[self._idx_of_list] = actual_data 

and here calling main program

if __name__ == "__main__":     my_list = []     thread1 = http_fetcher()     thread1.set_idx(1)     thread1.start()      thread2 = http_fetcher()     thread2.set_idx(2)     thread2.start()      thread3 = http_fetcher()     thread3.set_idx(3)     thread3.start() 

so far have not worked on reading site. think? writing different indexes of list should thread safe or wrong? how signalize thread list values have changed? error "nameerror: global name 'my_list' not defined" why that?

i hope of can , finds interesting

regards

https://docs.python.org/2/library/queue.html#module-queue

you may use queue module exchange information safely between multiple threads. there example @ end of module description.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -