Python asyncio

  • 有这样一个场景,需要异步的更新svn库的各个文件夹,因为svn update整个大目录耗时会很长,所以分而治之,每个小目录“同时”update
  • Code
# @Time    : 2023/11/10 16:02
# @Author  : yhw
# @FileName: c_update.py
# @Description:
import threading
import asyncio
import subprocess
import os
from loguru import logger

logger.add('c_svnupdate.log')

local = r"E:\data"
remote = r"https://1.game.com/svn/products/trunk/client/%s"


def thread_loop_task(loop):
    # 为子线程设置自己的事件循环
    asyncio.set_event_loop(loop)

    async def work_2():
        while True:
            print('work_2 on loop:%s' % id(loop))
            await asyncio.sleep(2)

    async def work_4():
        while True:
            print('work_4 on loop:%s' % id(loop))
            await asyncio.sleep(4)

    future = asyncio.gather(work_2(), work_4())
    # future = asyncio.gather(work_2())
    loop.run_until_complete(future)


def thread_loop_update(loop, local_directory, remote_directory):
    asyncio.set_event_loop(loop)

    async def update_svn():
        svn_show_last_changed_revision = "svn info --show-item last-changed-revision %s"
        local_version = svn_show_last_changed_revision % local_directory
        remote_version = svn_show_last_changed_revision % remote_directory

        while True:
            logger.info("{} begin update svn", local_directory)
            lv = subprocess.check_output(local_version, shell=True)
            logger.info("get svn local version {}", lv)
            rv = subprocess.check_output(remote_version, shell=True)
            logger.info("get svn remote version {}", rv)
            if int(rv) > int(lv):
                try:
                    w_update = "svn update %s" % local_directory
                    subprocess.check_output(w_update, shell=True)
                    # logger.info("data_bd update finish {}", update_result)
                except Exception as e:
                    w_c_up = "svn cleanup %s" % local_directory
                    os.system(w_c_up)
                    logger.info("update error {}", local_directory)
                    logger.info("update error {}", e)
            await asyncio.sleep(2)

    future = asyncio.gather(update_svn())
    loop.run_until_complete(future)


if __name__ == '__main__':

    # 创建一个事件循环thread_loop
    thread_loop = asyncio.new_event_loop()

    # 将thread_loop作为参数传递给子线程
    t = threading.Thread(target=thread_loop_task, args=(thread_loop,))
    t.daemon = True
    t.start()

    data_bd_thread_loop = asyncio.new_event_loop()
    # 将thread_loop作为参数传递给子线程
    remote_addr = remote % "data_bd"
    data_bd_t = threading.Thread(target=thread_loop_update, args=(data_bd_thread_loop, r"E:\data_bd", remote_addr,))
    data_bd_t.daemon = True
    data_bd_t.start()

    maps_thread_loop = asyncio.new_event_loop()
    # 将thread_loop作为参数传递给子线程
    remote_addr = remote % "maps"
    maps_t = threading.Thread(target=thread_loop_update, args=(maps_thread_loop, r"E:\maps", remote_addr,))
    maps_t.daemon = True
    maps_t.start()

    ui_thread_loop = asyncio.new_event_loop()
    # 将thread_loop作为参数传递给子线程
    remote_addr = remote % "ui"
    ui_t = threading.Thread(target=thread_loop_update, args=(ui_thread_loop, r"E:\ui", remote_addr,))
    ui_t.daemon = True
    ui_t.start()

    # 主事件循环
    main_loop = asyncio.get_event_loop()


    async def main_work():
        while True:
            print('main on loop:%s' % id(main_loop))
            await asyncio.sleep(4)


    main_loop.run_until_complete(main_work())

你可能感兴趣的:(Python,python,开发语言)