dotask 0.2.1__tar.gz → 0.2.3__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dotask
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Summary: 发布订阅注解方式
5
5
  Home-page: https://gitee.com/d-yz/task-manager
6
6
  Author: dyz
@@ -4,22 +4,34 @@ import subprocess
4
4
  from typing import Optional,Callable
5
5
 
6
6
  class Shell:
7
- def __init__(self,max_concurrent:int):
8
- self.max_concurrent = max_concurrent
9
- self.shell_semaphore = threading.Semaphore(max_concurrent)
7
+ _shell_semaphore:threading.Semaphore = None
8
+ _semaphore_lock = threading.Lock()
10
9
 
11
- def set_max_concurrent(self,new_max:int) -> None:
12
- if new_max <= 0:
13
- raise ValueError("最大并发数必须大于0")
14
- self.max_concurrent = new_max
15
- self.shell_semaphore = threading.Semaphore(new_max)
16
- logger.debug(f"最大并发数已更新为:{new_max}")
10
+ def __init__(self,max_concurrent:int,global_sem=False):
11
+ self.global_sem = global_sem
12
+ if global_sem:
13
+ if max_concurrent <= 0:
14
+ raise ValueError("最大并发数必须大于0")
15
+ self._shell_semaphore = threading.Semaphore(max_concurrent)
16
+ self._max_concurrent = max_concurrent
17
+ else:
18
+ with Shell._semaphore_lock:
19
+ if Shell._shell_semaphore is None:
20
+ if max_concurrent <= 0:
21
+ raise ValueError("最大并发数必须大于0")
22
+ Shell._shell_semaphore = threading.Semaphore(max_concurrent)
23
+ self._max_concurrent = max_concurrent
24
+ else:
25
+ if Shell._shell_semaphore._value != max_concurrent:
26
+ logger.warning(f"全局信号量已存在(并发数:{Shell._shell_semaphore._value}),忽略传入的max_concurrent={max_concurrent}")
27
+ self._max_concurrent = Shell._shell_semaphore._value
17
28
 
18
29
  def local_shell_execute(self,cmd:str,callback: Optional[Callable[[bool, str], None]] = None) -> bool:
19
30
  def execute_and_wait():
20
- # 关键:信号量放在线程内,覆盖整个命令执行周期
21
- with self.shell_semaphore:
22
- logger.debug(f"剩余并发名额: {self.shell_semaphore._value} | 开始执行命令: {cmd[:50]}")
31
+ semaphore = self._shell_semaphore if self.global_sem else Shell._shell_semaphore
32
+
33
+ with semaphore:
34
+ logger.debug(f"实例:{id(self)}-剩余并发名额: {semaphore._value} | 开始执行命令: {cmd[:50]}")
23
35
  proc = None
24
36
  try:
25
37
  # 创建并执行命令
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dotask
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Summary: 发布订阅注解方式
5
5
  Home-page: https://gitee.com/d-yz/task-manager
6
6
  Author: dyz
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = dotask
3
- version = 0.2.1
3
+ version = 0.2.3
4
4
  author = dyz
5
5
  author_email = 837701454@qq.com
6
6
  description = 发布订阅注解方式
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes