Python向线程发送变量

Python向线程发送变量,python,multithreading,thrift,Python,Multithreading,Thrift,我正试图在Python2.7中创建自己的线程类。我希望它能够用我自己的类函数停止该线程。目前我有这样的想法: class loop(threading.Thread): def __init__(self, myvar): super(loop, self).__init__() self.terminate = False self.myvar = myvar def run(self): while not self.terminate:

我正试图在Python2.7中创建自己的线程类。我希望它能够用我自己的类函数停止该线程。目前我有这样的想法:

class loop(threading.Thread):
  def __init__(self, myvar):
    super(loop, self).__init__()
    self.terminate = False
    self.myvar = myvar

  def run(self):
    while not self.terminate:
      do.smthng.useful(self.myvar)

  def change(self, newvar):
    self.myvar = newvar #Doesnt work, in run() my old var is still being used

  def stoploop(self):
    self.terminate = True #Also not working

l = loop(1)
l.start()
time.sleep(1)
l.change(2) #thread still using "1"
time.sleep(1)
l.stoploop() #doesnt stop
我在这里读过一些关于这个的帖子,但这不是我需要的。 任何帮助都将不胜感激。 多谢各位

编辑: 正如一些评论者已经指出的,这部分代码看起来真的很有效!问题出在我项目的另一个地方。我找到了,但解决不了。也许你们中的一些人可以帮忙

因此,我的项目使用Apache Thrift库,服务器使用python。 Server.py:

loo = loop(0)
handler = ServHandler(loo)
processor = serv.Processor(handler)
transport = TSocket.TServerSocket('0.0.0.0', port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory)
print 'Starting the server...'
server.serve()
ServHandler.py:

class ServHandler:
  def __init__(self, loo):
    self.loo = loo

  def terminate(self):  #Function that can be called remotely
    self.loo.stoploop() #Doesn't work
 class ServHandler:
  def __init__(self, loo):
    self.loo = None

  def terminate(self):  #Function that can be called remotely
    self.loo.stoploop() #Does work!!!!!!

  def create(self):
    self.loo = loop(0)  #Function that can be called remotely
在上述情况下,线程没有终止,我不知道为什么。没有错误,对象存在,但它在其他地方设置self.terminate值。对象id似乎与内存地址相同,但看起来对象不同,尽管循环init函数只调用一次

下面是循环成功终止时的示例。 ServHandler.py:

class ServHandler:
  def __init__(self, loo):
    self.loo = loo

  def terminate(self):  #Function that can be called remotely
    self.loo.stoploop() #Doesn't work
 class ServHandler:
  def __init__(self, loo):
    self.loo = None

  def terminate(self):  #Function that can be called remotely
    self.loo.stoploop() #Does work!!!!!!

  def create(self):
    self.loo = loop(0)  #Function that can be called remotely
当我远程创建循环对象时,我可以远程终止它。但它不适合我。在thrift server提供服务之前,应该创建一个线程,并且多个用户必须能够更改该线程的vars/terminate/etc。我怎样才能做到这一点?
谢谢大家!

不是每个sae的答案,而是OP的有用调试代码

from time import sleep
from threading import Thread

class loop(Thread):
    def __init__(self, myvar):
        Thread.__init__(self)
        self.terminate = False
        self.myvar = myvar

    def run(self):
        while self.terminate is False:
            print('Run says myvar is:',self.myvar)
            sleep(0.5)

    def change(self, newvar):
        self.myvar = newvar

    def stoploop(self):
        self.terminate = True

l = loop(1)
l.start()
sleep(1)
l.change(2)
sleep(1)
l.stoploop()
print('Final product:',l.myvar)
sleep(2)
print('Is the thread alive:',l.isAlive())
用一些调试打印件尝试了您的代码,它正常工作了吗?
生成以下代码:

[torxed@archie ~]$ python test.py 
Run says myvar is: 1
Run says myvar is: 1
Run says myvar is: 2        <-- Proves that change() does change `myvar`
Run says myvar is: 2
Final product: 2            <-- Also the global scope knows about the change
Is the thread alive: False  <-- And the thread got terminated as intended
此外,在更新数据时,您应该在数据上获得锁,以便不会发生冲突,请查看对象。

这是您需要的吗

import threading
import time

class Worker(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.state = threading.Condition()
        self.variable = 10
        self.paused = False

    def run(self):
        while True:
            with self.state:
                if self.paused:
                    self.state.wait()
            self.do_stuff()

    def do_stuff(self):
        time.sleep(.1)
        print self.variable

    def resume(self):
        with self.state:
            self.paused = False
            self.state.notify()

    def pause(self):
        with self.state:
            self.paused = True


loop = Worker()
loop.start()
time.sleep(1)

loop.pause()
loop.variable = 11
print 'CHANGED!'
loop.resume()
time.sleep(1)

你需要使用一个什么让你觉得你的代码“不起作用”的方法?老实说,你所做的听起来太过分了。虽然你可以使用节俭,但有更好更简单的方法。或者你们计划使用节俭的原因有并没有上面并没有提到的具体原因?JensG,这个项目已经完成了80%,并且非常依赖节俭了。。。