Skip to content

Instantly share code, notes, and snippets.

@ranman
Created July 17, 2011 03:43
Show Gist options
  • Save ranman/1087135 to your computer and use it in GitHub Desktop.
Save ranman/1087135 to your computer and use it in GitHub Desktop.
Idea for constantly updating dict
from multiprocessing import Process, Lock
class ConstantlyUpdatingDict(dict):
updater #= Process(target=update_dict, args=(self, self.data_lock))
data_lock #=
def __init__(self, *args):
dict.__init__(self,*args,**kwargs)
self.updater.start()
def __setitem__(self, key, val):
data_lock.acquire()
try:
dict.__setitem__(self, key, val)
finally:
data_lock.release()
def start_updating(self):
updater.start()
def stop_updating():
updater.join()
def update_dict(db, lock):
temp_db = {
# Grab new data here
}
lock.acquire()
try:
db[] = temp_db[]
db[] = temp_db[]
finally:
lock.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment