Are there any Python controls like this?

Asked 2 years ago, Updated 2 years ago, 15 views

Among control statements

Executing each command according to the conditions through various conditions

It's an if door, elif door, else door

However, you execute a command with one condition

It's done in order

It's very simple in this way

If it doesn't meet the conditions in the middle, it ends the function there I want the same thing

For example,

import time
from threading import Thread

def func1()
     global off
     while off:
        1. Moramora
        2. Moramora
        3. Moramora
        4. Moramora

t1=Thread(target=func1)
t1.start()

time.sleep(10)
off=0

For example, if you have this kind of code.

If the off value changes after 10 seconds, right?

So the func1 function ends, right?

What I want here is the moment when the value of off changes

Either number one or number two of that func1 function breaks off in the middle

Usually, it ends after running up to 4 times.

You can designate it as an if statement one by one.

I think there's a simple way

python

2022-09-20 20:47

1 Answers

If it's a simple way, I think there's a way to kill the thread instead of changing the value of off.

However, since threads cannot be killed, multiprocessing was used to perform the same action in the above case.

import time
import multiprocessing

def func1():
     while True:
        print(1)
        print(2)
        print(3)
        print(4)
        print(5)
        print(6)

if __name__ == '__main__':
    multiprocessing.freeze_support()
    t1=multiprocessing.Process(target=func1)
    t1.start()
    time.sleep(1)
    t1.terminate()

Thank you.


2022-09-20 20:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.