小能豆

How to trigger a 2nd script to run simultaneously without affecting the Main script and with the possibility of adding multiple additional scripts?

py

Note: I am updating my original post on 12/22/23 to better explain what is it that I am trying to achieve. Perhaps, this will make it easier to provide some suggestions.

I am trying to build a notification bot! This bot will run continuously, checking every 3 min if a given item has restocked. If it has, it will send an email to a group of recipients to notify them about the restock. What I now want to do, is to split said group between a preferred group (which will be notified as soon as the bot finds the item back in stock) and a second group which will receive the same email but 10 min later (delayed notification).

The rest of my post still stands. Script 2 will wait 10 min and then send an email (which would take 2-5 seconds) and then exit. I do not need Script 2’s result to run my main script. So I really do not need to keep track of Script 2 once it begins waiting.

As per some of the initial comments. At no point should there be more than 3 instances of Script 2 running simultaneously. Given that Script 2 waits for 10 min and then takes 3-5 seconds to complete its instructions, even if the Main Script finds the item in stock every 3 minutes, by the 4th time it finds the item (12 min since the first time), the first instance of Script 2 should have already ended. I hope that makes sense.

After reading about threading I came up with this super simple example of what I believe would be the structure of my scrip. Can someone tell me if this is wrong? Will I run out of resources if I run this script endlessly?

import threading
import time
import random

def delayed_email():
    time.sleep(10)
    print ("Delayed Email has been sent")

def immediate_email():
    print ("Immediate Email has been sent")

while True:
    number = random.randint(0,3)
    print (number)
    if number == 1: 
        immediate_email()
        print ("Starting Script 2")
        threading.Thread(target=delayed_email).start()
    time.sleep(3)

----Original Post -----

I need help figuring out, conceptually, how to write a script that would do the following:

Main Script: Run continuously, checking every 3 minutes if a certain condition has been met. If condition has been met, then run some code and immediately trigger Script 2. After this, go back to sleeping for 3 minutes. Rinse and repeat either if the condition is met or not.

Script 2: If triggered by Main Script, if would sleep for 10 min and then run some code. Once finished, it is done. Main Script does not depend on this script. But Script 2 will only run if triggered by Main Script.

Questions about this: I do not think I can use async, in this scenario right? I believe this is more for concurrency, either multithread or multiprocessor, I just do not know which one and why. Or perhaps I’m wrong and I need to organize my logic some other way.

Also, Script 2 should be it’s own entity that will live and die as soon as it finishes its code. This means that, for example:

  1. Main script runs and condition is met.
  2. Main script runs code and then triggers Script 2
  3. Main scrpit goes back to waiting for 3 minutes. While this is happening, Script 2 is sleeping for 10 minutes
  4. Main script’s condition is met again 3 minutes later, hence it runs its code and immediately triggers a 2nd entity of Script 2 (since the first entity is still sleeping waiting for the initial 10 min to elapse)

In the example above, there are now two instances of Script 2 running simultaneously. One with 7 min left to sleep, while the new one is just starting to sleep 10 min, all while Main Script is going back to waiting for another 3 min.

With all this in mind, would multithread work or better to use multiprocessor? If none of this would work, what other options do I have to organize the logic to trigger Script 2 parallelly.


阅读 76

收藏
2023-12-23

共1个答案

小能豆

For the described scenario, you can use a combination of multithreading and a task scheduler to achieve your goal. You can use the threading module for creating and managing threads and the schedule module for scheduling tasks at specific intervals.

Here’s a simplified example of how you could structure your code:

import threading
import time
import schedule

def script_2():
    print("Script 2 is running")
    time.sleep(600)  # Sleep for 10 minutes
    print("Script 2 has finished")

def main_script():
    # Check the condition every 3 minutes
    schedule.every(3).minutes.do(check_condition)

    while True:
        schedule.run_pending()
        time.sleep(1)

def check_condition():
    # Your condition-checking logic here
    condition_met = True  # Replace this with your actual condition-checking logic

    if condition_met:
        run_code()
        threading.Thread(target=script_2).start()

def run_code():
    print("Main script is running code")

if __name__ == "__main__":
    threading.Thread(target=main_script).start()

Explanation:

  1. main_script: This function runs continuously and checks the condition every 3 minutes using the schedule module.
  2. check_condition: This function checks the condition, runs the code if the condition is met, and triggers a new thread for script_2.
  3. script_2: This is the function that runs in a separate thread. It sleeps for 10 minutes and then performs its tasks.

This way, you can have multiple instances of script_2 running simultaneously, each in its own thread. The schedule module helps you manage the timing of the condition checks without having to use explicit sleep statements.

Remember to install the schedule module if you haven’t already:

pip install schedule

Keep in mind that the actual condition-checking logic, as well as the code to be executed in the main script (run_code), should be adapted to your specific requirements.

2023-12-23