在Windows中的Python 3.42中暂停并恢复正在运行的脚本

时间:2014-10-24 09:26:59

标签: python

我是Python的新手,已经谷歌搜索了几天,阅读了我在这个论坛上可以找到的所有内容。我可能不会理解这一切,但我还没有找到解决问题的方法。如果已经对我的问题有了答案,请求原谅,然后我就无法理解。

我想为我的程序Tennismatch制作暂停功能。该计划将在其运行时打印如下网球比赛的得分:" 15-0,15-15等,直到比赛结束。它将逐行打印得分。

我希望用户能够在x个球,游戏等之后暂停。所以我不知道用户何时想要暂停,并且在用户暂停后我希望用户能够恢复网球场的位置。

已经看过time.sleep()但是我已经明白了你必须知道你什么时候想要暂停使用它,这也是我想要的一个无法停顿。使用input()它是相同的。

代码完成后,我将在以后制作GUI。对于导致我解决问题的任何事情感到高兴。

我使用Windows和Python 3.42并在Shell中运行该程序。

一段代码(还没有写完所有内容,更多的是一般情况下,当一些线路打印一段时间并希望能够在CIL中暂停时:



#self.__points = [0,0]
def playGame(self):
    if self.server == True:       #self.server is either True or False when someone calls playGame()
        server = self.player_1.get_win_serve()   #self.player_1 = an object of a class Player():
    else:
        server = self.player_2.get_win_serve()   #get_win_serve() method returns the probability to win his serv (1-0)
    while (0 < self.__points[0] - self.__points[1] >= 2 or 0 < self.__points[1] - self.__points[0] >= 2) and (self.__points[1] >= 4 or self.__points[0] >= 4): 
        x = random.uniform(0,1)    
        if x > 0 and x < server:
            self.__points[0] += 1
            
        else:
            self.__points[1] += 1

            # print('The score, by calling a score() function that I haven't written yet')
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

我想出了以下内容。

 while True:
        try:
            ## Keep doing something here
            ## your regular code
            print '.',

        except KeyboardInterrupt:
            ## write or call pause function which could be time.sleep()
            print '\nPausing...  (Hit ENTER to continue, type quit to exit.)'
            try:
                response = raw_input()
                if response.lower() == 'quit':
                    break
                print 'Quitting...'
            except KeyboardInterrupt:
                print 'Resuming...'
                continue

答案 1 :(得分:0)

为了处理主循环中的事件,您需要创建一个捕获输入或任何其他事件的单独线程。

import sys
from sys import stdin
from time import sleep
from threading  import Thread
from Queue import Queue, Empty


def do_something():
    sleep(1)
    print 42


def enqueue_output(queue):
    while True:
        # reading line from stdin and pushing to shared queue
        input = stdin.readline()
        print "got input ", input
        queue.put(input)


queue = Queue()
t = Thread(target=enqueue_output, args=(queue,))
t.daemon = True
t.start()

pause = False

try:
    while True:
        try:
            command = queue.get_nowait().strip()
            print 'got from queue ', command
        except Empty:
            print "queue is empty"
            command = None

        if command:
            if command == 'p':
                pause = True

            if command == 'u':
                pause = False

        if not pause:
            print pause
            do_something()

except KeyboardInterrupt:
    sys.exit(0)

答案 2 :(得分:0)

  

Event循环可能也是我写的代码。

我没有看到任何用户输入,所以我假设x模仿它。要暂停游戏x < 0.1并暂停(/恢复)x > 0.9,您可以:

while your_condition(self.__points): 
    x = random.random()
    if x < 0.1: # pause
       self.pause()
    elif x > 0.9: # resume
       self.resume()
    if self.is_paused:
       continue # do nothing else only wait for input (`x`)
                # assume your_condition() has no side-effects

    # here's what the resumed version does:
    print("...")
    # change self.__points, etc

其中pause()resume()is_paused()方法可以实现为:

def __init__(self):
    self.is_paused = False
def pause(self):
    self.is_paused = True
def resume(self):
    self.is_paused = False

正如您所看到的,实现非常简单。

相关问题