python后台运行怎么操作_如何使用Python在后台运行方法

我试图在python(一个由aws服务器的消息不断更新的类)的后台运行一个方法,并将其用作模板。问题是,我不能让它打印('checkpoint')或print('bye')。它一直在跑。为什么?在import threading

import time

class ThreadingExample(object):

""" Threading example class

The run() method will be started and it will run in the background

until the application exits.

"""

def __init__(self, interval=1):

""" Constructor

:type interval: int

:param interval: Check interval, in seconds

"""

self.interval = interval

thread = threading.Thread(target=self.run, args=())

thread.daemon = True # Daemonize thread

thread.start() # Start the execution

def run(self):

""" Method that runs forever """

while True:

# Do something

print('Doing something imporant in the background')

time.sleep(self.interval)

example = ThreadingExample()

time.sleep(3)

print('Checkpoint')

time.sleep(2)

print('Bye')

编辑:我忘了提到我在Ubuntu14.04LTS64位上使用的是Python2.7.6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值