xtn-tools-pro 1.0.0.2.7__py3-none-any.whl → 1.0.0.2.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,36 +8,38 @@
8
8
  # --------------------------------------------------------------------------------------------------
9
9
  # 2025/1/14 xiatn V00.01.000 新建
10
10
  # --------------------------------------------------------------------------------------------------
11
- import os
12
- import configparser
13
- from xtn_tools_pro.utils.file_utils import check_file_exists
11
+ import time
12
+ import inspect
13
+ import requests
14
+ import threading
15
+ import multiprocessing
16
+ import concurrent.futures
17
+ from multiprocessing import Process
14
18
 
15
19
 
16
20
  class GoFun:
17
- def __init__(self, ini_dict):
21
+ def __init__(self, ini_dict, logger, go_task_function=None):
18
22
  # 读取配置信息
19
23
  host = ini_dict.get('host', '')
20
24
  port = ini_dict.get('port', 0)
21
25
  task = ini_dict.get('task', '')
22
26
  auto = ini_dict.get('auto', '')
23
- is_processes = ini_dict.get('is_processes', False)
24
27
  processes_num = ini_dict.get('processes_num', 0)
25
- is_thread = ini_dict.get('is_thread', False)
26
28
  thread_num = ini_dict.get('thread_num', 0)
29
+ restart_time = ini_dict.get('restart_time', 0)
27
30
 
28
31
  self.__ini_info = {
29
32
  "host": host,
30
33
  "port": port,
31
34
  "task": task,
32
35
  "auto": auto,
33
- "is_processes": is_processes,
34
36
  "processes_num": processes_num,
35
- "is_thread": is_thread,
36
37
  "thread_num": thread_num,
38
+ "restart_time": restart_time,
37
39
  }
38
40
 
39
41
  for server_k, server_v in self.__ini_info.items():
40
- if not server_v and server_k not in ["port", "is_processes", "processes_num", "is_thread", "thread_num"]:
42
+ if not server_v and server_k not in ["port", "processes_num", "thread_num", "restart_time"]:
41
43
  raise Exception(f"ini_dict 配置 {server_k} 不存在")
42
44
 
43
45
  if port:
@@ -47,8 +49,160 @@ class GoFun:
47
49
 
48
50
  download_url = task_host + "/filter_server/phone/get"
49
51
  upload_url = task_host + "/filter_server/phone/update"
52
+ external_ip = self.__get_external_ip()
50
53
 
51
54
  self.__ini_info["download_url"] = download_url
52
55
  self.__ini_info["upload_url"] = upload_url
56
+ self.__ini_info["external_ip"] = external_ip
53
57
 
54
- print(self.__ini_info)
58
+ self.logger = logger
59
+
60
+ if not go_task_function:
61
+ return
62
+
63
+ # 共享任务队列
64
+ download_queue = multiprocessing.Queue()
65
+ upload_queue = multiprocessing.Queue()
66
+ download_task_process = multiprocessing.Process(target=self._download_and_upload_task,
67
+ args=(download_queue, upload_queue, self.__ini_info, logger))
68
+ download_task_process.start()
69
+
70
+ # 根据配置启动任务
71
+ if go_task_function:
72
+ go_task_fun_process = multiprocessing.Process(target=self._go_task_fun_task,
73
+ args=(download_queue, upload_queue, self.__ini_info,
74
+ go_task_function, logger))
75
+ go_task_fun_process.start()
76
+
77
+ def __get_external_ip(self):
78
+ """
79
+ 获取当前网络ip
80
+ :return:
81
+ """
82
+ while True:
83
+ try:
84
+ rp = requests.get('https://httpbin.org/ip')
85
+ rp_json = rp.json()
86
+ print(f"当前网络ip --> {rp_json}")
87
+ return rp_json['origin']
88
+ except Exception as e:
89
+ pass
90
+
91
+ def _download_and_upload_task(self, download_queue, upload_queue, ini_info, logger):
92
+ """
93
+ 使用两个线程 打开 获取任务、回写任务
94
+ :param queue:
95
+ :return:
96
+ """
97
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
98
+ caller_name = caller.function # 获取调用者的函数名
99
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
100
+ if caller_name != "run" or caller_class is None:
101
+ raise Exception("错误调用")
102
+ thread_download_task = threading.Thread(target=self.__download_task, args=(download_queue, ini_info, logger))
103
+ thread_download_task.start()
104
+ thread_upload_task = threading.Thread(target=self.__upload_task, args=(upload_queue, ini_info, logger))
105
+ thread_upload_task.start()
106
+
107
+ def __download_task(self, download_queue, ini_info, logger):
108
+ """
109
+ 获取任务
110
+ :param queue:
111
+ :return:
112
+ """
113
+ download_url = ini_info["download_url"]
114
+ external_ip = ini_info["external_ip"]
115
+ auto = ini_info["auto"]
116
+ task = ini_info["task"]
117
+ headers = {"Authorization": auto}
118
+ params = {"taskType": task}
119
+ while True:
120
+ qsize = download_queue.qsize()
121
+ logger.info(f"获取任务,当前队列任务数:{qsize}")
122
+ if download_queue.qsize() >= 10:
123
+ time.sleep(2)
124
+ continue
125
+ resp = requests.get(download_url, headers=headers, params=params, timeout=5)
126
+ json_data = resp.json()
127
+ result_list = json_data.get("result", [])
128
+ for task_item in result_list:
129
+ download_queue.put(task_item)
130
+ logger.info(f"成功获取任务个数:{len(result_list)}")
131
+
132
+ def __upload_task(self, queue, ini_info, logger):
133
+ """
134
+ 回写任务
135
+ :return:
136
+ """
137
+ upload_url = ini_info["upload_url"]
138
+ external_ip = ini_info["external_ip"]
139
+ auto = ini_info["auto"]
140
+ task = ini_info["task"]
141
+ headers = {"Authorization": auto}
142
+ params = {"taskType": task}
143
+ while True:
144
+ qsize = queue.qsize()
145
+ print("回写任务", qsize)
146
+ time.sleep(1)
147
+
148
+ def _go_task_fun_task(self, download_queue, upload_queue, ini_info, go_task_function, logger):
149
+ """
150
+ 单函数,根据配置启动程序
151
+ :param queue:
152
+ :return:
153
+ """
154
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
155
+ caller_name = caller.function # 获取调用者的函数名
156
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
157
+ if caller_name != "run" or caller_class is None:
158
+ raise Exception("错误调用")
159
+
160
+ processes_num = ini_info["processes_num"]
161
+ thread_num = ini_info["thread_num"]
162
+ restart_time = ini_info["restart_time"]
163
+
164
+ processes_num = 1 if processes_num <= 0 else processes_num
165
+ thread_num = 1 if thread_num <= 0 else thread_num
166
+
167
+ go_task_fun_cnt = 0
168
+ processes_start_list = []
169
+ while True:
170
+ try:
171
+ if not processes_start_list:
172
+ go_task_fun_cnt += 1
173
+ logger.info(
174
+ f"第{go_task_fun_cnt}次,进程数:{processes_num},线程数:{thread_num},等待{restart_time}秒强制下一次")
175
+ for i in range(processes_num):
176
+ p = Process(target=self._run_with_timeout,
177
+ args=(download_queue, upload_queue, thread_num, go_task_function))
178
+ processes_start_list.append(p)
179
+ p.start()
180
+
181
+ if not restart_time:
182
+ # 一直执行 不退出
183
+ continue
184
+
185
+ time.sleep(restart_time)
186
+ # 关闭所有进程
187
+ for p in processes_start_list:
188
+ p.terminate()
189
+ p.join() # 等待进程确实结束
190
+ processes_start_list = []
191
+
192
+ except Exception as e:
193
+ pass
194
+
195
+ def _run_with_timeout(self, download_queue, upload_queue, thread_num, go_task_function):
196
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
197
+ caller_name = caller.function # 获取调用者的函数名
198
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
199
+ if caller_name != "run" or caller_class is None:
200
+ raise Exception("错误调用")
201
+
202
+ with concurrent.futures.ThreadPoolExecutor(max_workers=thread_num) as executor:
203
+ # 提交10个函数到线程池中执行
204
+ futures = [executor.submit(go_task_function, download_queue, upload_queue) for _ in range(thread_num)]
205
+
206
+ # 等待所有线程完成
207
+ for future in concurrent.futures.as_completed(futures):
208
+ future.result()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.0.2.7
3
+ Version: 1.0.0.2.8
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
6
  Author-email: czw011122@gmail.com
@@ -10,7 +10,7 @@ xtn_tools_pro/proxy/XiaoXiangProxy.py,sha256=45SsMZDc-3Ta4THX38vScjaeRBJSp420AJw
10
10
  xtn_tools_pro/proxy/__init__.py,sha256=WRwh6s2lruMu5buh0ejo9EK54kWT_VQhCsFGNFAmcyo,418
11
11
  xtn_tools_pro/proxy/proxy.py,sha256=No6E1pFY5yx2F4976pXPrLtq-QEVp79KupzcufjSN58,8703
12
12
  xtn_tools_pro/task_pro/__init__.py,sha256=nK3U47hWwE1H875ieEToH9r-jzXHS-PXk8cDstOvRE8,418
13
- xtn_tools_pro/task_pro/go_fun.py,sha256=x6RxA7wsqTfhlzlieJ3Q-sj9jz_ALanEMWTXIzWDBGE,1950
13
+ xtn_tools_pro/task_pro/go_fun.py,sha256=CccaMMl-Mr_SFO2kUSXH2JKsgNpvE90bxcMDfrqris8,8444
14
14
  xtn_tools_pro/utils/__init__.py,sha256=I1_n_NP23F2lBqlF4EOlnOdLYxM8M4pbn63UhJN1hRE,418
15
15
  xtn_tools_pro/utils/crypto.py,sha256=RZ5AET4udlraACWMeNF-17JiZ2R6Ahb47_j4tjkV7LE,3190
16
16
  xtn_tools_pro/utils/file_utils.py,sha256=VfdIxog4s1UW5NpKkCvQsUs9qHjLoNCnstZbnftkT4w,2046
@@ -19,8 +19,8 @@ xtn_tools_pro/utils/log.py,sha256=XZChGrKWgAz42o_gFi_d7DL28svF1OvAw7NS_xkIgnQ,10
19
19
  xtn_tools_pro/utils/retry.py,sha256=0wjHsR5DBBKpv4naMfxiky8kprrZes4WURIfFQ4H708,1657
20
20
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
21
21
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
22
- xtn_tools_pro-1.0.0.2.7.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- xtn_tools_pro-1.0.0.2.7.dist-info/METADATA,sha256=6oMhQBQO3wynl_-fvRDrKegTZyQqGiWe4y0UK-h-dik,455
24
- xtn_tools_pro-1.0.0.2.7.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
- xtn_tools_pro-1.0.0.2.7.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
- xtn_tools_pro-1.0.0.2.7.dist-info/RECORD,,
22
+ xtn_tools_pro-1.0.0.2.8.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
+ xtn_tools_pro-1.0.0.2.8.dist-info/METADATA,sha256=vPJnzEL5yxvzlRNSY18RO7fUZTtk-FQmTaHZ8MVEzKA,455
24
+ xtn_tools_pro-1.0.0.2.8.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
+ xtn_tools_pro-1.0.0.2.8.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
+ xtn_tools_pro-1.0.0.2.8.dist-info/RECORD,,