不能将线程分配给局部变量B

2024-10-01 17:24:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写了以下代码:

class gateWay:
   def __init__(self):
        self.var1 = []
        self.var2 = {}
        self.currentThread = None

   def stateProcess(self, file):
     # some irrelevant code 
     self.currentThread = saltGatWayThread(self, file).start()
     return self.var1

   def stopRunning(self):
      self.currentThread.proc.stop()

此外,这里是saltGatWayThread的源代码:

class saltGatWayThread(threading.Thread):
    def __init__(self):
       threading.Thread.__init__(self)
       # some irrelevant code
       self.proc = src.proc.Process1()

此外,我在src/proc/__init__.py中有以下代码:

class Process1:
     def stop(self):
          # code to stop operation

在控制台里,我注意到自流螺纹为空。你知道吗

我的目的是在启动线程时将其保存在局部变量中。如果我收到中止请求,我会申请 stopRunning函数。此函数,将采取保存的线程,并将做“清洁”退出(完成进程的踏板和退出)。你知道吗

为什么我不能保存线程,以后再使用它的结构呢?你知道吗


Tags: 代码selfinitdefcodesomeproc线程
1条回答
网友
1楼 · 发布于 2024-10-01 17:24:47

调用currentThread=saltGatWayThread(),然后调用.start()。currentThread不包含线程实例,因为starts()方法根据螺纹.py源代码。请参阅C:\Python27\Lib的源代码\螺纹.py def启动(自身): “”“启动线程的活动。你知道吗

    It must be called at most once per thread object. It arranges for the
    object's run() method to be invoked in a separate thread of control.

    This method will raise a RuntimeError if called more than once on the
    same thread object.

    """
    if not self.__initialized:
        raise RuntimeError("thread.__init__() not called")
    if self.__started.is_set():
        raise RuntimeError("threads can only be started once")
    if __debug__:
        self._note("%s.start(): starting thread", self)
    with _active_limbo_lock:
        _limbo[self] = self
    try:
        _start_new_thread(self.__bootstrap, ())
    except Exception:
        with _active_limbo_lock:
            del _limbo[self]
        raise
    self.__started.wait()

相关问题 更多 >

    热门问题