È possibile che più processi batch scrivano in modo sicuro in un singolo file di registro. Non so nulla di Python, ma immagino che i concetti di questa risposta possano essere integrati con Python.
Windows consente al massimo un processo di aprire un file specifico per l'accesso in scrittura in qualsiasi momento. Questo può essere utilizzato per implementare un meccanismo di blocco basato su file che garantisce che gli eventi siano serializzati su più processi. Vedere https://stackoverflow.com/a/9048097/1012053 e http://www.dostips.com/forum/viewtopic.php?p=12454 per alcuni esempi.
Poiché tutto ciò che si sta tentando di fare è scrivere su un registro, è possibile utilizzare il file di registro stesso come blocco. L'operazione di registro è incapsulata in una subroutine che tenta di aprire il file di registro in modalità append. Se l'open fallisce, la routine torna indietro e ci riprova. Una volta aperto, il log viene scritto e quindi chiuso e la routine ritorna al chiamante. La routine esegue qualunque comando gli venga passato e qualsiasi cosa scritta sullo stdout all'interno della routine viene reindirizzato al log.
Ecco uno script batch di test che crea 5 processi figlio che ciascuno scrive nel file di registro 20 volte. Le scritture sono intercalate in modo sicuro.
@echo off
setlocal
if "%~1" neq "" goto :test
:: Initialize
set log="myLog.log"
2>nul del %log%
2>nul del "test*.marker"
set procCount=5
set testCount=10
:: Launch %procCount% processes that write to the same log
for /l %%n in (1 1 %procCount%) do start "" /b "%~f0" %%n
:wait for child processes to finish
2>nul dir /b "test*.marker" | find /c "test" | >nul findstr /x "%procCount%" || goto :wait
:: Verify log results
for /l %%n in (1 1 %procCount%) do (
<nul set /p "=Proc %%n log count = "
find /c "Proc %%n: " <%log%
)
:: Cleanup
del "test*.marker"
exit /b
==============================================================================
:: code below is the process that writes to the log file
:test
set instance=%1
for /l %%n in (1 1 %testCount%) do (
call :log echo Proc %instance% says hello!
call :log dir "%~f0"
)
echo done >"test%1.marker"
exit
:log command args...
2>nul (
>>%log% (
echo ***********************************************************
echo Proc %instance%: %date% %time%
%*
(call) %= This odd syntax guarantees the inner block ends with success =%
%= We only want to loop back and try again if redirection failed =%
)
) || goto :log
exit /b
Ecco l'output che dimostra che tutte le 20 operazioni di scrittura hanno avuto successo per ogni processo
Proc 1 log count = 20
Proc 2 log count = 20
Proc 3 log count = 20
Proc 4 log count = 20
Proc 5 log count = 20
È possibile aprire il file risultante "myLog.log" per vedere come le scritture sono state in modo sicuro interlacciati. Ma l'output è troppo grande per postare qui.
È facile dimostrare che le scritture simultanee da più processi possono fallire modificando la routine: log in modo che non torni in errore.
:log command args...
>>%log% (
echo ***********************************************************
echo Proc %instance%: %date% %time%
%*
)
exit /b
Ecco alcuni risultati del campione dopo "rompere" il: log di routine
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
Proc 1 log count = 12
Proc 2 log count = 16
Proc 3 log count = 13
Proc 4 log count = 18
Proc 5 log count = 14
Quando apri un file su Windows (con CreateFile) puoi scegliere se altri processi possono leggere e/o scrivere sullo stesso file, il secondo processo deve specificare flag di condivisione compatibili ... – Anders
Abbastanza corretto. Stavo principalmente parlando di come il batch di Windows funziona con il reindirizzamento - nessuna opzione di cui sono a conoscenza. Più processi possono leggere, anche mentre un processo sta scrivendo. (Non sono sicuro che sia sempre sicuro). Ma mai più di un processo aperto per scrivere. – dbenham
@dbenham: Grazie mille per questo. In realtà risolve un altro grosso problema che ho. Potrebbe anche risolvere il mio problema di registrazione. La sintassi '>> file (comandi)' è documentata ovunque? – Omnifarious