How to prevent a race condition when multiple processes attempt to write to and then read from a file at the same time
问题 I have the following code (simplified for clarity): import os import errno import imp lib_dir = os.path.expanduser('~/.brian/cython_extensions') module_name = '_cython_magic_5' module_path = os.path.join(lib_dir, module_name + '.so') code = 'some code' have_module = os.path.isfile(module_path) if not have_module: pyx_file = os.path.join(lib_dir, module_name + '.pyx') # THIS IS WHERE EACH PROCESS TRIES TO WRITE TO THE FILE. THE CODE HERE # PREVENTS A RACE CONDITION. try: fd = os.open(pyx_file,