python类中怎么编写多线程方法

import random
import time
from concurrent.futures import ThreadPoolExecutor
poll = ThreadPoolExecutor(2)

class Demo():
    def __init__(self):
        self.data = None
        self.running = True
        self.future = poll.submit(self.make_data)

    def make_data(self):
        while self.running:
            self.data = random.randint(1, 100)

    def get_data(self):
        return self.data

    def stop_data(self):
        self.running = False


if __name__ == '__main__':
    obj = Demo()
    time.sleep(2)
    print(obj.get_data())
    time.sleep(2)
    print(obj.get_data())
    time.sleep(5)
    print(obj.get_data())
    print(dir(obj.future))
    obj.stop_data()
    print(333)

原创文章,转载请注明出处:http://124.221.219.47/article/13248/