简体   繁体   English

在第二次执行时停止 function 的第一次执行(Python)

[英]Halting first execution of function on second execution (Python)

I have a function that is called when the page loads but can also be called through a button.我有一个 function 在页面加载时调用,但也可以通过按钮调用。 When the page loads, the function is called - it simply waits for 15 seconds and then terminates.当页面加载时,调用 function - 它只是等待 15 秒然后终止。 On the other hand, if a user presses the button before the 15 seconds are up, the function is called again and instantly terminates.另一方面,如果用户在 15 秒结束前按下按钮,则再次调用 function 并立即终止。 Is there any way for this button press to halt the first call of the function?按下此按钮有什么方法可以停止 function 的第一次调用?

#the first execution is called with the default value for the "chosen" argument.
#the second one (on button press) is always called with a non-zero value for "chosen"

def background_calculation(self, chosen=0):
    if chosen == 0:
        time.sleep(15)
        pos = np.random.randint(1, 54)
        return pos
    else:
        pos = chosen
        #I would like to stop the first function call from continuing to execute here.
        return pos

Context: background_calculation is called when a thread opens.上下文:当线程打开时调用background_calculation The user has 15 seconds to make a selection, and if they do not the thread should close with a random value for pos .用户有 15 秒的时间进行选择,如果他们不这样做,线程应该以pos的随机值关闭。 On the other hand, if the user makes a selection before the 15 seconds are up, the function is called and the thread is immediately ended with the user-selected value returned.另一方面,如果用户在 15 秒结束之前做出选择,则调用 function 并立即结束线程并返回用户选择的值。 At present, the function executes twice and returns two values, the user-chosen one and the randomly generated one.目前function执行两次,返回两个值,用户选择的一个和随机生成的一个。

What I tried: I tried using an indicator/dummy variable which points to the latest value of "chosen".我尝试了什么:我尝试使用指向“选择”的最新值的指标/虚拟变量。 At the end of the 15 seconds, the function would check if the dummy variable still points to 0 (indicating chosen was never changed) and would halt if it isn't.在 15 秒结束时,function 将检查虚拟变量是否仍指向 0(表明所选择的从未更改过),如果不是,则会停止。

You probably want to maintain a global state:您可能想要维护一个全局 state:

button_clicked = False

def background_calculation(self, chosen=0):
    if chosen == 0:
        for i in range(15):
            time.sleep(1)
            if button_clicked: # user clicks the button
                break
        else:
            pos = np.random.randint(1, 54)
            return pos
    
    else: # chosen = 1
        global button_clicked
        button_clicked = True
        
        pos = np.random.randint(1, 54)
        return pos

Note that this implementation is just showing you how we detect the button clicking.请注意,此实现只是向您展示我们如何检测按钮单击。

Warning: this is not thread-safe to be called directly.警告:直接调用这不是线程安全的。

If you only need to run only one instance at a time, you can this with a lock:如果您一次只需要运行一个实例,则可以使用锁:

from threading import Lock

with Lock():
    background_calculation(self, chosen=0) # or 1

If you need multiple calculations to be done at the same time (from multiple users clicking the button), you need to put locks inside the function to make sure the state is set and get correctly.如果您需要同时进行多项计算(来自多个用户单击按钮),您需要在 function 内加锁,以确保 state 设置正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM