Skip to content

Commit 0f613ef

Browse files
authored
tccli支持多进程并发 (#107)
* tccli支持多进程并发 * fix test case * 新增 os.rename逻辑 * fix lint
1 parent ca74bba commit 0f613ef

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

tccli/configure.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
2+
import copy
33
import os
44
import sys
55
import six
@@ -47,6 +47,9 @@ def _init_configure(self, profile_name, input_data, extra={}):
4747
is_exist, config_path = self._profile_existed(profile_name)
4848
if is_exist:
4949
conf_data = Utils.load_json_msg(config_path)
50+
old_data = copy.deepcopy(conf_data)
51+
else:
52+
old_data = None
5053

5154
if profile_name.endswith(".configure") and OptionsDefine.SysParam not in conf_data:
5255
conf_data[OptionsDefine.SysParam] = {}
@@ -84,7 +87,8 @@ def _init_configure(self, profile_name, input_data, extra={}):
8487
"Received input format: %s\n "
8588
"Valid input format eg. set cvm.version 2017-03-12"
8689
% (err, k))
87-
Utils.dump_json_msg(config_path, conf_data)
90+
if conf_data != old_data:
91+
Utils.dump_json_msg(config_path, conf_data)
8892

8993
def _checkout_config(self, profile_name):
9094
is_conexit, config_path = self._profile_existed(profile_name + ".configure")

tccli/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,3 @@ def main():
9090

9191
if __name__ == "__main__":
9292
main()
93-

tccli/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import time
55
import os
6+
import uuid
67
import tccli.options_define as options_define
78

89

@@ -84,12 +85,19 @@ def dump_json_msg(filename, data):
8485
file_dir = os.path.split(filename)[0]
8586
if not os.path.isdir(file_dir):
8687
os.makedirs(file_dir)
87-
with open(filename, "w") as f:
88+
89+
temp_file = filename + uuid.uuid4().hex + ".tmp"
90+
with open(temp_file, "w") as f:
8891
json.dump(data, f,
8992
indent=2,
9093
separators=(',', ': '),
9194
ensure_ascii=False,
9295
sort_keys=True)
96+
try:
97+
os.rename(temp_file, filename)
98+
except Exception:
99+
os.remove(temp_file)
100+
raise Exception("write configure file failed")
93101

94102
@staticmethod
95103
def get_call_mode():

tests/test_cvm.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from multiprocessing import Process, Queue
2+
from tccli.command import CLICommand
13
from utils import TestCli
24

35
def test_describe_regions():
@@ -51,3 +53,24 @@ def test_create_image_dry_run_with_unfold_argument():
5153
test_cli = TestCli()
5254
test_cli.equal(cmd, expect)
5355

56+
57+
def test_multi_process():
58+
queue = Queue()
59+
def run_cvm_describe_regions(idx, q):
60+
args = ["cvm", "DescribeRegions"]
61+
try:
62+
CLICommand()(args)
63+
q.put(0)
64+
except Exception:
65+
q.put(255)
66+
67+
processes = []
68+
for i in range(10):
69+
p = Process(target=run_cvm_describe_regions, args=(i, queue))
70+
p.start()
71+
processes.append(p)
72+
for p in processes:
73+
p.join()
74+
while not queue.empty():
75+
ret = queue.get()
76+
assert ret == 0

0 commit comments

Comments
 (0)