绞线...如何正确使用它们? [英] Threads in twisted... how to use them properly?

查看:102
本文介绍了绞线...如何正确使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个运行两个线程的简单应用程序: -线程1:每隔一定时间运行一次 -线程2:执行填充"的True循环只是一个正常"

I need to write a simple app that runs two threads: - thread 1: runs at timed periods, let's say every 1 minute - thread 2: just a 'normal' while True loop that does 'stuff'

如果不是要求以一定的时间间隔运行,我根本不会看扭曲的,但是简单的sleep(60)不够好,构造如下:

if not the requirement to run at timed interval I would have not looked at twisted at all, but simple sleep(60) is not good enough and construction like:

l = task.LoopingCall(timed_thread)
l.start(60.0)
reactor.run()

在这里实现我想要的东西真的很简单.

Looked really simple to achieve what I wanted there.

现在,如何适当地"添加另一个线程?

Now, how do I 'properly' add another thread?

我在这里看到两个选项:

I see two options here:

  • 使用线程库并运行两个"python线程",一个执行我的while循环,另一个运行Reactor.run().但是Google似乎反对这种方法,并建议使用扭曲线程
  • 使用双绞线.这就是我尝试过的方法,但在某种程度上,这对我来说显得有些笨拙.

这是我想出的:

def timed_thread():
    print 'i will be called every 1 minute'
    return

def normal_thread():
    print 'this is a normal thread'
    time.sleep(30)
    return

l = task.LoopingCall(timed_thread)
l.start(60.0)
reactor.callInThread(normal_thread)
reactor.run()

这似乎有效,但是!我无法停止该应用程序.如果我按^ C键,则不会执行任何操作(如果没有'callInThread',它将按您期望的那样停止运行). ^ Z向外壳轰炸,如果我然后执行"kill%1"操作,则似乎会杀死该进程(shell报告该操作),但正常"线程继续运行.杀死PID并不能消除它,唯一的解决方法是杀死-9.真的很奇怪.

That seems to work, but! I can't stop the app. If I press ^C it wouldn't do anything (without 'callInThread' it just stops as you'd expect it to). ^Z bombs out to shell, and if I then do 'kill %1' it seems to kill the process (shell reports that), but the 'normal' thread keeps on running. kill PID wouldn't get rid of it, and the only cure is kill -9. Really strange.

所以.我究竟做错了什么?用双绞线实现两个线程是正确的方法吗?我不应该为扭曲而烦恼吗?还有什么其他标准"替代方法可以实现定时呼叫? (标准"是指我可以easy_install或yum安装它们,我不想开始从随机网页上下载并使用一些随机脚本.)

So. What am I doing wrong? Is it a correct approach to implement two threads in twisted? Should I not bother with twisted? What other 'standard' alternatives are to implement timed calls? ('Standard' I mean I can easy_install or yum install them, I don't want to start downloading and using some random scripts from random web pages).

推荐答案

假设您的主体相对无阻塞:

Assuming that your main is relatively non-blocking:

import random
from twisted.internet import task

class MyProcess:
  def __init__(self):
    self.stats = []
    self.lp = None
  def myloopingCall(self):
    print "I have %s stats" % len(self.stats)
  def myMainFunction(self,reactor):
    self.stats.append(random.random())
    reactor.callLater(0,self.myMainFunction,reactor)
  def start(self,reactor):
    self.lp = task.LoopingCall(self.myloopingCall)
    self.lp.start(2)
    reactor.callLater(0,self.myMainFunction,reactor)
  def stop(self):
    if self.lp is not None:
      self.lp.stop()
    print "I'm done"

if __name__ == '__main__':
  myproc = MyProcess()
  from twisted.internet import reactor
  reactor.callWhenRunning(myproc.start,reactor)
  reactor.addSystemEventTrigger('during','shutdown',myproc.stop)
  reactor.callLater(10,reactor.stop)
  reactor.run()


$ python bleh.py
I have 0 stats
I have 33375 stats
I have 66786 stats
I have 100254 stats
I have 133625 stats
I'm done

这篇关于绞线...如何正确使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆