M
M
MazAlVlad2020-06-10 14:18:43
Python
MazAlVlad, 2020-06-10 14:18:43

Why doesn't multiprocessing.Lock block file access?

Initially, I planned to skip one process in to_log in order to keep a general log of processes. But in an incomprehensible way, the lines in the log jump, either an extra transition, or vice versa, there is no transition character \n.
I rewrote only the necessary piece, 'my_log.txt' will be formed, everything is visible there. I have:
2020-06-10 13:52:02 4FILE_BOT.txt RUNNIN2020-06-10 13:52:02 2_FILE.txt RUNNING
2020-06-10 13:52:02 1_FILE.txt RUNNING
G
2020-06-10 13:52:02 2_FILE.txt RUNNING
2020-06-10 13:52:02 2_FILE.txt RUNNING
G
2020-06-10 13:52:02 2_FILE.txt RUNNING
G
2020-06-10 13:52:02 2_FILE .txt RUNNING
In short, such a horror. And now the code itself;

from multiprocessing import Process,current_process,Lock#,BoundedSemaphore
from time import strftime,localtime,sleep

#LOCK_PROCESS_SEMAPHORE=BoundedSemaphore(value=1)
LOCK_PROCESS=Lock()
    
def to_log(FILE_LOG,STRING):
    LOCK_PROCESS.acquire()
    #LOCK_PROCESS_SEMAPHORE.acquire()                                       #занять семафор
    
    MY_TIME=strftime("%Y-%m-%d %H:%M:%S",localtime())                       #просто время
    STRING=MY_TIME+'\t'+'\t'+str(current_process().name)+'\t'+STRING        #просто к времени добавилось имя процесса
    print(STRING)                                                           #просто печать в консоль
    STRING=STRING+'\n'                                                      #просто добавление \n в строку
        
    #LOCK_PROCESS_SEMAPHORE.acquire()                                       #занять семафор

    '''
    with open (FILE_LOG,'a') as f:
        f.write(STRING)                                                     #просто сохранение строки
    '''
    FILE=open (FILE_LOG,'a')
    FILE.write(STRING)
    FILE.close()
        
    #LOCK_PROCESS_SEMAPHORE.release()                                       #освободить семафор
    LOCK_PROCESS.release() 

def my_processes():
    FILE_LOG='my_log.txt'
    while True:
        STRING='RUNNING'
        sleep(0.001)
        to_log(FILE_LOG,STRING)
        continue
    return
   
def start_PROCESS():
    MY_FILES=['1_FILE.txt', '2_FILE.txt', '4FILE_BOT.txt']                  #Это типо будущие процессы
    for MY_FILE in MY_FILES:
        Process_MY_BOT=Process(target=my_processes,name=MY_FILE)
        Process_MY_BOT.start()
   
if __name__ =='__main__':
    start_PROCESS()
    input('end')


Found a working solution:
from multiprocessing import Process,current_process,Lock
from time import strftime,localtime,sleep
    
def to_log(FILE_LOG,STRING,LOCK_PROCESS):
    LOCK_PROCESS.acquire()
    
    MY_TIME=strftime("%Y-%m-%d %H:%M:%S",localtime())                       #просто время
    STRING=MY_TIME+'\t'+'\t'+str(current_process().name)+'\t'+STRING        #просто к времени добавилось имя процесса
    print(STRING)                                                           #просто печать в консоль
    STRING=STRING+'\n'                                                      #просто добавление \n в строку

    FILE=open (FILE_LOG,'a')
    FILE.write(STRING)
    FILE.close()
        
    LOCK_PROCESS.release() 

def my_processes(LOCK_PROCESS):
    FILE_LOG='my_log.txt'
    while True:
        STRING='RUNNING'
        sleep(0.001)
        to_log(FILE_LOG,STRING,LOCK_PROCESS)
        continue
   
def start_PROCESS():
    LOCK_PROCESS=Lock()
    MY_FILES=['1_FILE.txt', '2_FILE.txt', '4FILE_BOT.txt']                  #Это типо будущие процессы
    for MY_FILE in MY_FILES:
        Process_MY_BOT=Process(target=my_processes,args=(LOCK_PROCESS,),name=MY_FILE)
        Process_MY_BOT.start()
   
if __name__ =='__main__':
    start_PROCESS()


The question is why is relevant, I can not understand the principle of the interpreter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-06-11
@MazAlVlad

Because blocking in multiprocessing is implemented using semaphores, and if it is not passed in the pool arguments, the corresponding handle will simply close when the worker process starts, and you will get just a bunch of unrelated locks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question