相关疑难解决方法(0)

Python在类中进行线程化

我最近开始使用python的线程模块.经过一些试验和错误后,我设法使用大多数教程中给出的以下示例代码来使基本线程工作.

class SomeThread(threading.Thread):
    def __init__(self, count):
        threading.Thread.__init__(self)

    def run(self):
        print "Do something"
Run Code Online (Sandbox Code Playgroud)

我的问题是:我有一个具有类变量的类和一个我希望在一个单独的线程中运行的函数.但是,该函数使用类变量并写入类变量.像这样:

class MyClass:
    somevar = 'someval'

    def func_to_be_threaded(self):
        # Uses other class functions
        # Do something with class variables
Run Code Online (Sandbox Code Playgroud)

那么我将如何将线程类放入MyClass中呢?因此,如果调用MyClass().func_to_threaded(),它将在一个线程中运行.

python multithreading

33
推荐指数
2
解决办法
6万
查看次数

标签 统计

multithreading ×1

python ×1