如何在一个脚本中调用另一个脚本并同时运行它们?

时间:2018-11-19 06:48:01

标签: python

我对Python非常陌生,但是我正在从事的项目使我感到困惑。 在项目中,我给出了多种选择供选择,其中一种具有提醒功能。因此,在提醒功能中,用户可以设置提醒,该功能将每15秒钟将提醒与当前时间进行匹配,直到它们匹配并打印一条语句。

这是提醒代码。

import time

def setReminder(number):
    reminderList = []
    for i in range(number):
        reminderL = []
        mon = input('enter the month(1-12):')
        day = input('enter the date(1-31):')
        hour = input('enter the hour(0-23):')
        minute = input('enter the minute(0-59):')
        print()

        if ((int(mon)<1 or int(mon)>12)
            or (int(day)<1 or int(day)>31)
            or (int(hour)<0 or int(hour)>23)
            or (int(minute)<0 or int(minute)>59)):
            print('invalid date and time, please set again!')
            mon = input('enter the month(1-12):')
            day = input('enter the date(1-31):')
            hour = input('enter the hour(0-23):')
            minute = input('enter the minute(0-59):')

        reminderL.extend((mon, day, hour, minute))
        reminder = ''
        for element in reminderL:
            if int(element)<10:
                element = '0' + element
            reminder = reminder + element + ' '
        reminderList.append(reminder)

    return reminderList

def main():
    num = int(input('enter the numbers of reminder you want to set(1-9):'))
    if num not in range(1,10):
        print('invalid input, try again!')
        num = int(input('enter the numbers of reminder you want to set(0-9):'))

    List = setReminder(num)
    print(List)
    for i in range(num):
        tim = time.strftime('%m %d %H %M ')
        while tim not in List:
            time.sleep(15)
            tim = time.strftime('%m %d %H %M ')
        print('Hello, it is time to take medicine!')
main()

但是,如果用户将提醒时间设置为很晚,例如第二天,则此功能将一直运行到第二天。因此,我希望在此提醒功能运行时运行正文脚本。

这通常是我的身体脚本的样子(它与另一个带有提醒的脚本相同):

menu()
option = int(input('Your choice?'))
while (option != 4):
    if option not in (1, 2, 3, 4):
        print('Invalid input! Please select again!')
        option = int(input('Your choice?'))

    else:
        if option == 1:
            print()

        elif option == 2:
            feedback(gender, dat)

        elif option == 3:
            print()

        print()
        print('Anything else?')
        menu()
        option = int(input('Your choice?'))

所以选项3是提醒,但是在提醒功能尝试匹配时间的同时,我希望用户能够使用其他选项。我认为可以工作的唯一方法是调用该函数并在另一个Shell页面上运行它。您能给我一些建议吗?

1 个答案:

答案 0 :(得分:1)

您需要的工具是threading。假设您有两个单独的脚本文件:reminder.pyprogram.py。然后,您必须在main()中的行reminder上删除或注释掉,并将reminderthreading导入program中。接下来是修改program

elif option == 3:
    rem_thread = threading.Thread(
        target=reminder.main)
    rem_thread.start()

这是如何在Python中使用线程的非常基本的示例。但是,由于以下两个原因,对代码的这种补充无法解决您的问题 :( 1)不同线程中input()调用之间的干扰; (2)被input()阻止。为了解决第一个问题,您应该在用户输入时间部分时暂停主线程。要解决第二个问题,您应该再启动一个线程。

这是可以工作的代码(Win10 /命令提示符)。有很多设计问题需要解决,以使代码或多或少地可用。我测试了1个余数,因为每个余数都需要单独的线程。 input()print()之间仍然存在干扰,因为多个线程共享同一控制台。可以使用进程而不是线程或GUI解决此问题。当然,GUI是更好的选择。

import queue
import threading
import time

def setReminder(number):
    reminderList = []
    for i in range(number):
        reminderL = []
        mon = input('enter the month(1-12):')
        day = input('enter the date(1-31):')
        hour = input('enter the hour(0-23):')
        minute = input('enter the minute(0-59):')
        print()

        if ((int(mon)<1 or int(mon)>12)
            or (int(day)<1 or int(day)>31)
            or (int(hour)<0 or int(hour)>23)
            or (int(minute)<0 or int(minute)>59)):
            print('invalid date and time, please set again!')
            mon = input('enter the month(1-12):')
            day = input('enter the date(1-31):')
            hour = input('enter the hour(0-23):')
            minute = input('enter the minute(0-59):')

        reminderL.extend((mon, day, hour, minute))
        reminder = ''
        for element in reminderL:
            if int(element)<10:
                element = '0' + element
            reminder = reminder + element + ' '
        reminderList.append(reminder)

    return reminderList

def reminder_main(wait_queue):
    num = int(input('enter the numbers of reminder you want to set(1-9):'))
    if num not in range(1,10):
        print('invalid input, try again!')
        num = int(input('enter the numbers of reminder you want to set(0-9):'))

    List = setReminder(num)
    print(List)
    wait_queue.put('go-go-go')  # unpause main thread
    for i in range(num):
        tim = time.strftime('%m %d %H %M ')
        while tim not in List:
            time.sleep(1)
            tim = time.strftime('%m %d %H %M ')
        print('Hello, it is time to take medicine!')


def main():
    while True:
        try:
            option = int(input('Your choice? '))
        except ValueError:
            print('Cannot convert your choice into integer.')
        else:
            if option not in (1, 2, 3, 4):
                print('Allowed options are 1, 2, 3, 4.')
            else:
                if option == 1:
                    pass
                if option == 2:
                    pass
                if option == 3:
                    wait_queue = queue.Queue()
                    # remainder thread
                    threading.Thread(
                        target=reminder_main,
                        kwargs={'wait_queue': wait_queue},
                        daemon=True).start()
                    wait_queue.get()  # this is pause - waitng for any data
                elif option == 4:
                    print('Exiting...')
                    break


if __name__ == '__main__':

    # main thread
    threading.Thread(
        target=main).start()