inter thread/process synchronisation from threading import threadlock class shareddata(object)def __init__(self)self value self lock lock(def read_value(self)tryprint('read_value acquiring lock'self lock acquire(return self value finallyprint('read_value releasing lock'self lock release(def change_value(self)print('change_value acquiring lock'with self lockself value self value print('change_value lock released'the shareddata class presented above uses locks to control access to critical blocks of codespecifically to the read_value(and the change_value(methods the lock object is held internally to the sharedata object and both methods attempt to acquire the lock before performing their behavior but must then release the lock after use the read_value(method does this explicitly using tryfinallyblocks while the change_value(method uses with statement (as the lock type supports the context manager protocolboth approaches achieve the same result but the with statement style is more concise the shareddata class is used below with two simple functions in this case the shareddata object has been defined as global variable but it could also have been passed into the reader(and updater(functions as an argument both the reader and updater functions loopattempting to call the read_value(and change_value(methods on the shared_data object as both methods use lock to control access to the methodsonly one thread can gain access to the locked area at time this means that the reader(function may start to read data before the updater(function has changed the data (or vice versathis is indicated by the output where the reader thread accesses the value ' twice before the updater records the value ' howeverthe updater(function runs second time before the reader gains access to locked block of code which is why the value is missed depending upon the application this may or may not be an issue