帮助管理长时间连续运行的线程的库。
continuous-threading的Python项目详细描述
#连续的线程
-
这个库提供了几个类来帮助管理连续运行的线程。
计算线程是贪婪的,并且一直在运行
,这会使其他线程挨饿。另一个问题是,如果不退出线程中的无限循环,则在python尝试退出后,它可能会继续运行
。守护进程线程将关闭,但资源/变量可能无法正确清理。
大多数情况下,我需要在线程关闭之前完成将数据写入文件的操作。此库旨在解决这些问题。
-可以停止并重新启动的连续线程。
*operationthread-将在具有不同数据的单独线程中运行计算的线程。
*periodicthread-以给定间隔定期运行函数的线程。
库将线程转换为一个上下文管理器,它自动启动和停止线程。
`` python
导入连续线程
thread\u success=[false]
def do\u something():
打印('here')
thread\u success[0]=true
continuous threading.thread(target=do_something):
print('in context')
assert thread_success[0]为true
````
continuousthread
continuousthread是无限while循环中的简单线程。while循环在设置thread
alive事件时保持循环。调用"thread.stop()"、"thread.close()"或"thread.join()"来停止线程。当python程序退出/关闭时,线程也应该自动停止。
`` python
导入连续线程
=0
def运行(self):
self.counter+=1
停止方法。
`` python
导入时间
导入连续线程
+=1
th=countingthread()
th.start()
time.sleep(0.1)
th.stop()或th.close()或th.join()
assert th.counter>;0
print("简单的上下文管理器打印导致%d个线程迭代"%th.counter)
```
`` pausablethread
可以停止并重新启动的连续线程。
`` python
导入时间
导入连续的线程
counter=[0]
definc-counter():
计数器[0]+=1
th=连续线程。可暂停线程(target=inc-counter)
th.start()
时间睡眠(0.1)
th.stop()
时间睡眠(0.1)
值=计数器[0]
断言值>;0
time.sleep(0.1)
assert value==计数器[0]
time.sleep(0.1)
assert counter[0]>;值
```
这同样可以用作上下文管理器。
``python
导入时间
导入连续线程
def运行(self):
self.counter+=1
th.stop()
value=th.counter
assert value>;0
time.sleep(0.1)
assert value==th.counter
th.start()
time.sleep(0.1)
assert th.counter>;值
```
数据2):
值。追加(数据1+数据2)
th=连续线程。操作线程(目标=运行计算)
th.start()
th.添加数据(1,1)
时间。睡眠(0.1)
断言长度(值)>;0
断言值[0]==2
th.添加数据(2,2)
th.添加数据(3,3)
th.添加数据(4,4)
th.添加数据(5,5)
time.睡眠(0.1)
断言值==[2,4,6,8,10]
```
节省时间)
th.start()
time.sleep(4)
th.join()
打印(时间列表)
```
-
这个库提供了几个类来帮助管理连续运行的线程。
计算线程是贪婪的,并且一直在运行
,这会使其他线程挨饿。另一个问题是,如果不退出线程中的无限循环,则在python尝试退出后,它可能会继续运行
。守护进程线程将关闭,但资源/变量可能无法正确清理。
大多数情况下,我需要在线程关闭之前完成将数据写入文件的操作。此库旨在解决这些问题。
-可以停止并重新启动的连续线程。
*operationthread-将在具有不同数据的单独线程中运行计算的线程。
*periodicthread-以给定间隔定期运行函数的线程。
库将线程转换为一个上下文管理器,它自动启动和停止线程。
`` python
导入连续线程
thread\u success=[false]
def do\u something():
打印('here')
thread\u success[0]=true
continuous threading.thread(target=do_something):
print('in context')
assert thread_success[0]为true
````
continuousthread
continuousthread是无限while循环中的简单线程。while循环在设置thread
alive事件时保持循环。调用"thread.stop()"、"thread.close()"或"thread.join()"来停止线程。当python程序退出/关闭时,线程也应该自动停止。
`` python
导入连续线程
=0
def运行(self):
self.counter+=1
停止方法。
`` python
导入时间
导入连续线程
+=1
th=countingthread()
th.start()
time.sleep(0.1)
th.stop()或th.close()或th.join()
assert th.counter>;0
print("简单的上下文管理器打印导致%d个线程迭代"%th.counter)
```
`` pausablethread
可以停止并重新启动的连续线程。
`` python
导入时间
导入连续的线程
counter=[0]
definc-counter():
计数器[0]+=1
th=连续线程。可暂停线程(target=inc-counter)
th.start()
时间睡眠(0.1)
th.stop()
时间睡眠(0.1)
值=计数器[0]
断言值>;0
time.sleep(0.1)
assert value==计数器[0]
assert counter[0]>;值
```
这同样可以用作上下文管理器。
``python
导入时间
导入连续线程
def运行(self):
self.counter+=1
th.stop()
value=th.counter
assert value>;0
time.sleep(0.1)
assert value==th.counter
th.start()
time.sleep(0.1)
assert th.counter>;值
```
数据2):
值。追加(数据1+数据2)
th=连续线程。操作线程(目标=运行计算)
th.start()
th.添加数据(1,1)
时间。睡眠(0.1)
断言长度(值)>;0
断言值[0]==2
th.添加数据(2,2)
th.添加数据(3,3)
th.添加数据(4,4)
th.添加数据(5,5)
time.睡眠(0.1)
断言值==[2,4,6,8,10]
```
节省时间)
th.start()
time.sleep(4)
th.join()
打印(时间列表)
```