I am trying to store logs from multiple threads into a single file. But in my log file, logs from different threads are getting merged. I don't want separate log files for different threads. But when one thread is writing logs to the file, the other thread should wait till the previous thread finishes writing all the logs. I have tried different ways to do so, but finally didn't find appropriate solution.
from threading import Thread, current_thread
import threading
import time
import logging
logging.basicConfig(filename='LogsThreadPrac.log', level=logging.INFO)
logger = logging.getLogger(__name__)
def worker(val):
print('value from the thread: {}'.format(val))
logger.info('+++++++++++++++++++++++++++++++++++++++')
print(current_thread().name)
logger.info('value from the thread: {}'.format(val))
time.sleep(3)
logger.info('+++++++++++++++++++++++++++++++++++++++')
t1 = Thread(target=worker, args=[1,], name='th'+str(1))
t1.start()
t2 = Thread(target=worker, args=[2,], name='th'+str(2))
t2.start()
You could pass a Lock as an argument to your workers, so you manage to synchronize the threads the way you want.
from threading import Thread, current_thread, Lock
import threading
import time
import logging
logging.basicConfig(filename='LogsThreadPrac.log', level=logging.INFO)
logger = logging.getLogger(__name__)
logger_lock = Lock()
def worker(val, lock):
print('value from the thread: {}'.format(val))
with lock:
logger.info('+++++++++++++++++++++++++++++++++++++++')
print(current_thread().name)
logger.info('value from the thread: {}'.format(val))
time.sleep(3)
logger.info('+++++++++++++++++++++++++++++++++++++++')
t1 = Thread(target=worker, args=[1, logger_lock], name='th'+str(1))
t1.start()
t2 = Thread(target=worker, args=[2, logger_lock], name='th'+str(2))
t2.start()
You can check more about the lock uses and examples here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With