I try to make selenium bot that integrate to telegram so i can start and stop easily
def mulai(update, context):
update.message.reply_text('Absen Start!')
while True:
if pycron.is_now('*/1 * * * *'):
absen()
time.sleep(60)
but how to stop it?
2
Answers
To stop the code from running you can use the break statement in python.
Without the break statement the code would continue counting up to 10.
Based on clarification in comments, you are trying to stop one loop from elsewhere. One approach is a shared variable:
This is safe so long as only one write access to
run_flag
can be made at any one time, i.e. you aren’t trying to share it accross multiple threads. If you are you need to look at using something thread safe, like asemaphore
.There is no need for
run_flag
to be global, as long it is can be accessed by both the looping function and the function which sets it.