How to wait for an input without blocking timer in python?

て烟熏妆下的殇ゞ 提交于 2019-12-06 14:26:48

You can use threading to print your message in a different thread.

import threading

t = threading.Timer(30,func,args=[])
t.start()

Where 30 is how often to call func.

func is the function to call in a different thread.

And args is an array of the arguments to call the function with

If you want just one call to a different function you can do

t = threading. Thread(target=func, args=[]) 
t.start() 

This will make func run in parallel

import threading
import time as t

value = 0

def takeInput():
    """This function will be executed via thread"""
    global value
    while True:
        value = raw_input("Enter value: ")
        if value == 'q':
            exit()  # kills thread
        print value
    return

if __name__ == '__main__':
    x = int(raw_input('time interval: '))
    thread = threading.Thread(target=takeInput)
    thread.start()
    while True:
        if value == 'q':
            exit()  # kills program
        print 'some message'
        t.sleep(x)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!