如何在循环中运行并仍然接受用户输入?

时间:2016-03-28 10:55:33

标签: python scheduler

def value(startH,startM,stopH,stopM):

        def job():
                do job 

        def job1():
                do another job


        start_time = "{0:02d}:{1:02d}".format(startH, startM)
        stop_time = "{0:02d}:{1:02d}".format(stopH, stopM)

        schedule.every().day.at(start_time).do(job)
        schedule.every().day.at(stop_time).do(job1)

        while True:
                schedule.run_pending()
                time.sleep(1)

这里startH,startM,stopH,stopM表示开始小时,开始分钟,停止小时和停止分钟。这是用户通过android给出的输入。此代码运行。它运行onces然后它继续运行。这是适合。如果我希望用户再次输入时间。它不会接受。如何在循环仍在运行时接受用户的输入?让我们说第一个任务是打开灯然后第二个任务就是关灯。所以当第二个任务完成时。它被认为是完整的。我尝试使用break,return。它没有按预期工作。

public void publish(int startH,int startM, int stopH, int stopM )
{
    JSONObject js = new JSONObject();
    try {
        js.put("START_HOUR", startH);
        js.put("START_MINUTE", startM);
        js.put("STOP_HOUR", stopH);
        js.put("STOP_MINUTE", stopM);



    }


public void setTime(View view)
{


    int storeStartHour = Integer.parseInt(startHrs.getText().toString());
    int storeStartMinutes = Integer.parseInt(startMinutes.getText().toString());
    int storeStopHour = Integer.parseInt(stopHrs.getText().toString());
    int storeStopMinutes = Integer.parseInt(stopMinutes.getText().toString());



     publish(storeStartHour, storeStartMinutes, storeStopHour, storeStopMinutes);



}

1 个答案:

答案 0 :(得分:1)

您可以使用threading

这是一个非常简单的例子:

导入线程 进口时间

def worker(num):
    # Do some stuff
    for i in range(5):
        time.sleep(2)
        print(2**(num + i))

if __name__ == "__main__":
    i = int(input("Enter a number: "))

    t = threading.Thread(target=worker, args=(i,)) # Always put a comma after the arguments. Even if you have only one arg.
    t.start() # Start the thread

    while True:
        choice = input()

        if choice == "stop":
            print("Waiting for the function to finish...")
            t.join() # Stop the thread (NOTE: the program will wait for the function to finish)
            break

        else:
            print(choice)

worker生成数字时,您仍然可以输入内容 除非你真的必须这样做,否则不要在worker函数中打印,因为stdout可能会变得混乱。