mémorandum de multitraitement

Mémorandum de multitraitement

L'environnement est Windows 10

Si vous souhaitez verrouiller dans le pool, créez un verrou à partir du gestionnaire

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 () a 2 logs en sortie standard

Manipuler que deux gestionnaires sont enregistrés?


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

J'ai essayé d'ajouter un gestionnaire de fichiers


from multiprocessing import Pool,Lock
import multiprocessing
import logging

logger = logging.getLogger()
#Générer un gestionnaire
file_handler = logging.FileHandler('test.log', 'a')
#Paramètre de niveau de journal du gestionnaire(Niveau de sortie du message d'erreur par le gestionnaire)
file_handler.setLevel(logging.DEBUG)
#Réglage du format de sortie du journal
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

mémorandum de multitraitement
Mémorandum Matplotlib
mémorandum Linux
mémorandum jinja2
Mémorandum Python
Mémorandum de commandement
Mémorandum Python 2
mémorandum complot
Mémorandum Slackbot (1)
Mémorandum MetaTrader5
Mémorandum ShellScript
mémorandum pip
Mémorandum Python
mémorandum pydoc
mémorandum python
Mémorandum de Pandas
mémorandum python
Mémorandum DjangoGirls
Multitraitement instantané
Mémorandum de commandement
Mémorandum Python
mémorandum pandas
mémorandum python
Mémorandum Python
Mémorandum de base Python
Mémorandum de type RAID
multitraitement vs threading
Mémorandum de problème de sac à dos
Mémorandum de sed
Mémorandum Python (algorithme)
Mémorandum Linux [liens]
mémorandum de commande nc
Mémorandum d'apprentissage profond
mémorandum numpy 1 / np.pad
Mémorandum d'API Revit
Commande mémorandum conda
Mémorandum d'opération Pandas
Mémorandum de commande setuptools
Mémorandum Python [liens]
Mémorandum de procès tslearn
Mémorandum de base de Django
Mémorandum sur la validation