- 有这样一个场景,需要异步的更新svn库的各个文件夹,因为svn update整个大目录耗时会很长,所以分而治之,每个小目录“同时”update
- Code
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())
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)
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 = asyncio.new_event_loop()
t = threading.Thread(target=thread_loop_task, args=(thread_loop,))
t.daemon = True
t.start()
data_bd_thread_loop = asyncio.new_event_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()
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()
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())