python多线程记录

在这里插入图片描述
线程是进程的基本单位。利用多线程进行执行代码,可以加快文件的处理速度,这里主要是利用多线程对若干图片进行裁剪操作。废话不多说,直接上代码!

import os
from PIL import Image
import time
from multiprocessing.pool import ThreadPool


def crop_mid(img,output_path,roots):
    root, name = os.path.split(roots)
    w,h = img.size
    if w<=h:
        img_f1 = img.crop((0,(h-w)//2,w,(h+w)//2))
    else:
        img_f1 = img.crop(((w-h)//2,0,(w+h)//2,h))

    img_f1.save(os.path.join(output_path,"crop_mid","default", name[:-4] + '_m.jpg'))


def crop_img_thread(img,output_path,roots,num_processes, Async = True):
    pool = ThreadPool(processes=num_processes)
    if Async:
        pool.apply_async(func=crop_mid, args=(img, output_path, roots))  #异步
    else:
        pool.apply(func=crop_mid, args=(img, output_path, roots))  #同步
    pool.close()
    pool.join()


def main():
    file_path = r'C:\Users\E14\Desktop\rotation_code/test_list.txt'
    output_path = r'C:\Users\E14\Desktop\test\data'
    with open(file_path) as f:
        file_list = f.readlines()
    for num,lis in enumerate(file_list):
        roots,cls = lis.strip().split(" ")
        img = Image.open(roots)
        trc = time.time()
        crop_img_thread(img, output_path, roots, num_processes=10)
        print("hello kitty!",num, "The time cost:", time.time()-trc, "s")

if __name__ == '__main__':
    main()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python 中,可以使用 `logging` 模块记录日志。如果需要在多线程环境下记录日志,可以使用 `concurrent_log_handler` 模块来实现。 首先需要安装 `concurrent_log_handler` 模块: ``` pip install concurrent_log_handler ``` 然后,可以使用以下代码来记录日志: ```python import logging from concurrent_log_handler import ConcurrentRotatingFileHandler import threading # 创建日志记录器 logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # 创建文件处理器 handler = ConcurrentRotatingFileHandler(filename='example.log', mode='a', maxBytes=1024, backupCount=3) handler.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # 将处理器添加到记录器 logger.addHandler(handler) # 多线程记录日志 def worker(): logger.debug('Debug message from thread {0}'.format(threading.current_thread().name)) logger.info('Info message from thread {0}'.format(threading.current_thread().name)) logger.warning('Warning message from thread {0}'.format(threading.current_thread().name)) logger.error('Error message from thread {0}'.format(threading.current_thread().name)) logger.critical('Critical message from thread {0}'.format(threading.current_thread().name)) threads = [] for i in range(10): t = threading.Thread(target=worker) threads.append(t) t.start() for t in threads: t.join() ``` 上面的代码中,创建了一个 `ConcurrentRotatingFileHandler` 类实例化的文件处理器,它可以在多线程环境下安全地写入日志文件。然后,将处理器添加到日志记录器中,并使用多线程记录日志。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WYKB_Mr_Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值