Script runs simultaneous with hotkey function

Asked by mark.w.325

I am running into an issue with a hotkey function. I am running this through the IDE and using version 1.0.1 on Windows 7. When I run this, I see the steps in my log file. When I press the hotkey to pause the script, it runs the pause_unpause_script function and writes to the log as it should. What happens next I cannot figure out. The script keeps running the steps and breaks where appropriate, but it should not be continuing. It should be stuck in the infinite loop of the pause function until I press the hotkey again.

Script:

import sys
import logging
import os
import __builtin__ as pyth
from datetime import *
from time import time, sleep
from math import *
from random import *
from sikuli.Sikuli import *

PAUSED = False

def setup_logger(file_name='me.log', file_mode='w+', logger_name=__name__):
    return_log = logging.getLogger(logger_name)
    return_log.setLevel(level=logging.INFO)
    handler = logging.FileHandler(filename=file_name, mode=file_mode)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M %p')
    handler.setFormatter(formatter)
    return_log.addHandler(handler)
    return_log.propagate = False
    return return_log

def intWithCommas(x):
    if pyth.type(x) not in [pyth.type(0), pyth.type(0L)]:
        raise TypeError("Parameter must be an integer.")
    if x < 0:
        return '-' + intWithCommas(-x)
    result = ''
    while x >= 1000:
        x, r = divmod(x, 1000)
        result = ",%03d%s" % (r, result)
    return "%d%s" % (x, result)

def pause_unpause_script(event=None):
    global PAUSED
    PAUSED = not PAUSED
    if PAUSED:
        logger.info('Pausing script...Press Pause to unpause...')
        while PAUSED:
            sleep(10)
            popup("Waited ten seconds")
    else:
        logger.info('Unpausing script...Press Pause Lock to pause...')
        # TODO: Add clicking of 'Confirm' box and event windows

# ========================== #
# ==== Add HotKeys ==== #
# ========================== #
Env.addHotkey(Key.F1, KeyModifier.CTRL, exit_script)
Env.addHotkey(Key.PAUSE, 0, pause_unpause_script)

logger = setup_logger(file_name="C:\\test.log")

i = 0
while True:
    if i >= 25:
        break
    i += 1
    logger_step = "Step: %s" % intWithCommas(i)
    logger.info(logger_step)
    sleep(2)

Log Output:

08/07/2015 10:25 AM - __main__ - INFO - Step: 1
08/07/2015 10:25 AM - __main__ - INFO - Step: 2
08/07/2015 10:25 AM - __main__ - INFO - Step: 3
08/07/2015 10:25 AM - __main__ - INFO - Step: 4
08/07/2015 10:25 AM - __main__ - INFO - Step: 5
08/07/2015 10:25 AM - __main__ - INFO - Step: 6
08/07/2015 10:25 AM - __main__ - INFO - Step: 7
08/07/2015 10:25 AM - __main__ - INFO - Step: 8
08/07/2015 10:25 AM - __main__ - INFO - Step: 9
08/07/2015 10:25 AM - __main__ - INFO - Step: 10
08/07/2015 10:25 AM - __main__ - INFO - Pausing script...Press Pause to unpause...
08/07/2015 10:25 AM - __main__ - INFO - Step: 11
08/07/2015 10:25 AM - __main__ - INFO - Step: 12
08/07/2015 10:25 AM - __main__ - INFO - Step: 13
08/07/2015 10:25 AM - __main__ - INFO - Step: 14
08/07/2015 10:25 AM - __main__ - INFO - Step: 15
08/07/2015 10:25 AM - __main__ - INFO - Step: 16
08/07/2015 10:25 AM - __main__ - INFO - Step: 17
08/07/2015 10:25 AM - __main__ - INFO - Step: 18
08/07/2015 10:25 AM - __main__ - INFO - Step: 19
08/07/2015 10:25 AM - __main__ - INFO - Step: 20
08/07/2015 10:25 AM - __main__ - INFO - Step: 21
08/07/2015 10:25 AM - __main__ - INFO - Step: 22
08/07/2015 10:26 AM - __main__ - INFO - Step: 23
08/07/2015 10:26 AM - __main__ - INFO - Step: 24
08/07/2015 10:26 AM - __main__ - INFO - Step: 25
08/07/2015 10:26 AM - __main__ - INFO - Unpausing script...Press Pause Lock to pause...

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
masuo (masuo-ohara) said :
#1

What you want to do, I think that the following.
I'm sorry I was wrong.

-------------------
def pause_unpause_script(event=None):
    global PAUSED
    PAUSED = not PAUSED

Env.addHotkey(Key.F1, KeyModifier.CTRL, exit_script)
Env.addHotkey(Key.PAUSE, 0, pause_unpause_script)

logger = setup_logger(file_name="C:\\test.log")

i = 0
while True:
    global PAUSED

    if PAUSED:
        logger.info('Pausing script...Press Pause to unpause...')
        sleep(10)
    else:
        logger.info('Unpausing script...Press Pause Lock to pause...')

    if i >= 25:
        break
    i += 1
    logger_step = "Step: %s" % intWithCommas(i)
    logger.info(logger_step)
    sleep(2)

Revision history for this message
mark.w.325 (maw325) said :
#2

Interesting. I had not thought to do it this way. I guess this is the real question, why does the sleep within the pause_unpause_script function not pause the script and forced to handle it this way?

This is one way to do it, but it will not pause the script indefinitely. I want it to pause until the user presses the hotkey again to unpause it.

Could I do something like this?:

if PAUSED:
    logger.info('Pausing script..Press Pause to unpause...')
    while PAUSED:
        sleep(10)
else:
    ....rest of code

Revision history for this message
masuo (masuo-ohara) said :
#3

i think that event handler runs asynchronously with the main script.
The sleep() within a event handler is effective for the event handler,but not effective for main script.

Revision history for this message
Launchpad Janitor (janitor) said :
#4

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

should stay open

Revision history for this message
Karl (k-d) said :
#6

You can pause and unpause your script with events. See below:

import threading
done = False

def exit_script(event):
    global done
    done = True
    if not my_event.isSet():
        my_event.set()

def pause_unpause_script(event):
    if my_event.isSet():
        print('Pausing script...Press Pause to unpause...')
        my_event.clear()
        #popup("Waited ten seconds")
    else:
        print('Unpausing script...Press Pause Lock to pause...')
        my_event.set()

my_event = threading.Event()
my_event.set()

Env.addHotkey(Key.F1, KeyModifier.CTRL, exit_script)
Env.addHotkey(Key.PAUSE, 0, pause_unpause_script)

i="+"
while not done:
    wait(.5)
    my_event.wait()
    print i

Env.removeHotkey(Key.F1, KeyModifier.CTRL)
Env.removeHotkey(Key.PAUSE, 0)

Can you help with this problem?

Provide an answer of your own, or ask mark.w.325 for more information if necessary.

To post a message you must log in.