[PYTHON] Multiprocessing Memorandum

Memorandum of Multiprocessing

Umgebung ist Windows 10

Wenn Sie den Pool sperren möchten, erstellen Sie die Sperre im Manager

from multiprocessing import Pool,Manager
import logging

LOGGER = multiprocessing.get_logger()
LOGGER.setLevel(logging.ERROR)

def f(lock,x):
    lock.acquire()
    try:
        LOGGER.error('hoge{}'.format(x))
    finally:
        lock.release()

    return x*x

def main():
    m = Manager()
    lock = m.Lock()
    with Pool(2) as p:
        jobs = [ p.apply_async(f, (lock,i)) for i in range(10) ]
        for job in jobs:
            print(job.get())

if __name__ == '__main__':
    main()

multiprocessing.log_to_stderr () enthält 2 Protokolle in der Standardausgabe

Umgang damit, dass zwei Handler registriert sind?


from multiprocessing import Pool,Lock
import multiprocessing
import logging

LOGGER = multiprocessing.log_to_stderr()
LOGGER.setLevel(logging.ERROR)

def f(lock,x):
    lock.acquire()
    try:
        LOGGER.error('hoge{}'.format(x))
    finally:
        lock.release()

    return x*x

def main():
    m = multiprocessing.Manager()
    lock = m.Lock()
    with Pool(2) as p:
        jobs = [ p.apply_async(f, (lock,i)) for i in range(5) ]
        for job in jobs:
            print(job.get())

if __name__ == '__main__':
    main()
[ERROR/SpawnPoolWorker-2] hoge0
[ERROR/SpawnPoolWorker-2] hoge0
0
[ERROR/SpawnPoolWorker-3] hoge1
[ERROR/SpawnPoolWorker-3] hoge1
1
[ERROR/SpawnPoolWorker-2] hoge2
[ERROR/SpawnPoolWorker-2] hoge2
4
[ERROR/SpawnPoolWorker-3] hoge3
[ERROR/SpawnPoolWorker-3] hoge3
9
[ERROR/SpawnPoolWorker-2] hoge4
[ERROR/SpawnPoolWorker-2] hoge4
16

Ich habe versucht, einen Datei-Handler hinzuzufügen


from multiprocessing import Pool,Lock
import multiprocessing
import logging

logger = logging.getLogger()
#Handler generieren
file_handler = logging.FileHandler('test.log', 'a')
#Einstellung der Handler-Protokollstufe(Ebene der vom Handler ausgegebenen Fehlermeldung)
file_handler.setLevel(logging.DEBUG)
#Einstellung des Protokollausgabeformats
handler_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(handler_format)
logger.addHandler(file_handler)

LOGGER = multiprocessing.log_to_stderr()
LOGGER.setLevel(logging.ERROR)
LOGGER.addHandler(file_handler)

def f(lock,x):
    lock.acquire()
    try:
        LOGGER.error('hoge{}'.format(x))
    finally:
        lock.release()

    return x*x

def main():
    m = multiprocessing.Manager()
    lock = m.Lock()
    logger.error('process start')
    with Pool(2) as p:
        jobs = [ p.apply_async(f, (lock,i)) for i in range(5) ]
        for job in jobs:
            print(job.get())

if __name__ == '__main__':
    main()
[ERROR/SpawnPoolWorker-3] hoge0
[ERROR/SpawnPoolWorker-3] hoge0
0
[ERROR/SpawnPoolWorker-2] hoge1
[ERROR/SpawnPoolWorker-2] hoge1
1[ERROR/SpawnPoolWorker-3] hoge2

[ERROR/SpawnPoolWorker-3] hoge2
4
[ERROR/SpawnPoolWorker-2] hoge3
[ERROR/SpawnPoolWorker-2] hoge3
9[ERROR/SpawnPoolWorker-3] hoge4

[ERROR/SpawnPoolWorker-3] hoge4
16

test.log


2020-05-02 17:21:59,128 - root - ERROR - process start
2020-05-02 17:21:59,968 - multiprocessing - ERROR - hoge0
2020-05-02 17:21:59,981 - multiprocessing - ERROR - hoge1
2020-05-02 17:21:59,987 - multiprocessing - ERROR - hoge2
2020-05-02 17:21:59,994 - multiprocessing - ERROR - hoge3
2020-05-02 17:22:00,002 - multiprocessing - ERROR - hoge4

Recommended Posts

Multiprocessing Memorandum
Matplotlib Memorandum
Linux Memorandum
jinja2 Memorandum
Python-Memorandum
Befehlsmemorandum
Python-Memorandum 2
Plotly Memorandum
Slackbot-Memorandum (1)
Memorandum MetaTrader5
ShellScript-Memorandum
Pip Memorandum
Python-Memorandum
Pydoc Memorandum
Python Memorandum
Memorandum of Pandas
Python Memorandum
DjangoGirls Memorandum
Sofortige Mehrfachverarbeitung
Befehlsmemorandum
Python-Memorandum
Pandas Memorandum
Python Memorandum
Python-Memorandum
Python-Grundmemorandum
Memorandum vom RAID-Typ
Multiprocessing vs Threading
Rucksack Problem Memorandum
Memorandum von sed
Python-Memorandum (Algorithmus)
Linux Memorandum [Links]
nc Befehl Memorandum
Deep Learning Memorandum
numpy memorandum 1 / np.pad
Revit API-Memorandum
Memorandum Conda Befehl
Pandas Operations Memorandum
setuptools Befehl Memorandum
Python-Memorandum [Links]
tslearn Trial Memorandum
Djangos grundlegendes Memorandum
Memorandum über die Validierung