python等待输入_Python:等待时接受输入

I have a small script that I have written to perform tasks every 5 minutes (this does not need to be exact), but as it is written currently the only way it can be cancelled is with Ctrl+Z (linux terminal) and there is no way of changing what set of tasks it performs without restarting the script.

As a result, I'd like to change the script to be able to accept user input while waiting to do the next run without interrupting the 5-minute timer by any large amount (as killing the script and restarting currently does), but I can't figure out how to get raw input to accept values for only a given time, etc. Also the current code is written in Python 2.7, but I'm happy to convert to 3 if that makes the solution easier.

The current code looks something like this:

# Setup for the tasks

while(True):

# Do the tasks

for i in range(5):

print "Next run in " + str(5-i) + " mins"

time.sleep(60)

Any ideas?

UPDATE: This question did not lead to a specific code solution being posted, so here'a a link to a later question that had a simple version of the solution I implemented (and the one simple edit that it required it to work in the answers)/

解决方案

I think you want something like that.

import threading

def takeInput():

"""This function will be executed via thread"""

value = raw_input("what you want:")

return

#initialize how you want

value = 0

t = threading.Thread(target=takeInput)

t.start()

time.sleep(5)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值