Created
July 17, 2011 03:43
-
-
Save ranman/1087135 to your computer and use it in GitHub Desktop.
Idea for constantly updating dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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