如何在上下文管理器中使用线程?

How to use a thread in a context manager?

考虑这个 threading.Thread class:

class Sleeper(threading.Thread):
    def __init__(self, sleep=5.0):
        threading.Thread.__init__(self)
        self.event = threading.Event()
        self.sleep = sleep

    def run(self):
        while self.sleep > 0 and not self.event.is_set():
            self.event.wait(1.0)
            self.sleep -= 1

    def stop(self):
        self.event.set()

它会休眠一段时间并在达到该时间之前退出或停止。

我将其用作:

sleeper = Sleeper()
try:
    sleeper.start()
    # do stuffs here
except:
    # handle possible exceptions here
finally:
    sleeper.stop()

而且我更愿意像上下文管理器一样使用它:

with Sleeper():
    # do stuffs here

然后线程在退出 with 块时停止。

我已经尝试添加 __enter____exit__ 方法,它似乎有效,但我不确定这是要走的路:

def __enter__(self):
    self.start()
    return self

def __exit__(self, type, value, traceback):
    self.stop()

但我真的不确定我在这里做什么。应该如何正确处理?

虽然不太理解你的问题,但由于缺乏与你的aws相关问题的背景。正如您提到的,使用上下文来执行此操作是可行的。

import threading
import time


class Sleeper(threading.Thread):
    def __init__(self, sleep=5.0):
        threading.Thread.__init__(self, name='Sleeper')
        self.stop_event = threading.Event()
        self.sleep = sleep

    def run(self):
        print('Thread {thread} started'.format(thread=threading.current_thread()))
        while self.sleep > 0 and not self.stop_event.is_set():
            time.sleep(1.0)
            self.sleep -= 1
        print('Thread {thread} ended'.format(thread=threading.current_thread()))

    def stop(self):
        self.stop_event.set()

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, *args, **kwargs):
        self.stop()
        print('Force set Thread Sleeper stop_event')


with Sleeper(sleep=2.0) as sleeper:
    time.sleep(5)

print('Main Thread ends')

你可以测试这两种情况:1. main sleep 时间更长, 2. Sleeper thread sleep parameter 更大,最终会有两种结果;

如果您仍想通过主线程与 Sleeper 线程交互,您的代码应如下所示:

with Sleeper(sleep=2.0) as sleeper:
    cnt = 15

    while cnt > 0 and sleeper.is_alive():
        print(cnt)
        cnt -= 1
        time.sleep(1)

而且你可以看到 main 只打印了几个数字,因为 sleeper 已经结束并且不再活着了。