Pause and resume a running script in Python 3.42 in Windows -
Pause and resume a running script in Python 3.42 in Windows -
i'm new python , have been googling couple of days , read can find on forum. might don't understand haven't found solution problem yet. inquire forgiveness if there's reply problem, haven't understood it.
i want create pause function programme tennismatch. programme when it's beingness run print score of lawn tennis match this: "15-0, 15-15 etc ongoing till match ends. print score line line.
i want user able pause after x number of balls, games, etc. don't know when user wants pause , after user has paused want user able resume tennismatch was.
have seen time.sleep() have understood must know when want pause utilize , ain't indefinetie pause want. input() it's same.
am going create gui later on when code finished. happy leads me solving problem.
i utilize windows , python 3.42 , run programme in shell.
a piece of code (haven't written yet, it's more of general situation when beingness printed line after line time , want able pause in cil:
class="snippet-code-html lang-html prettyprint-override">#self.__points = [0,0] def playgame(self): if self.server == true: #self.server either true or false when calls playgame() server = self.player_1.get_win_serve() #self.player_1 = object of class player(): else: server = self.player_2.get_win_serve() #get_win_serve() method returns probability win serv (1-0) while (0 < self.__points[0] - self.__points[1] >= 2 or 0 < self.__points[1] - self.__points[0] >= 2) , (self.__points[1] >= 4 or self.__points[0] >= 4): x = random.uniform(0,1) if x > 0 , x < server: self.__points[0] += 1 else: self.__points[1] += 1 # print('the score, calling score() function haven't written yet')
for dealing events in main loop need create separated thread capture input or other event.
import sys sys import stdin time import sleep threading import thread queue import queue, empty def do_something(): sleep(1) print 42 def enqueue_output(queue): while true: # reading line stdin , pushing 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 queue ', command except empty: print "queue 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)
python
Comments
Post a Comment