Python Forum

Full Version: Schedule with other processing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I create a schedule and continue with the parallel processing of other functions

sched = BlockingScheduler(timezone='UTC')
def my_job1():
    # stuff

sched.add_job(my_job1, 'cron', id='Job', minute=1)
sched.start()

def waiting():
    # function that is executed only when recive command
Have you tried using Threads?
Python3 Threading
not yet, would you have an example of how you would run a function every 10 minutes?
import time
import threading


def my_function(n):
    """This is My Funtion"""
    print(f'Counter: {n}')


def main():
    """Start a Thread every x_minutes"""

    try:
        x_minutes = 10
        counter = 1
        while True:
            t = threading.Thread(target=my_function, args=(counter,))
            t.start()
            counter += 1
            time.sleep(60 * x_minutes)

    except KeyboardInterrupt:
        print('Leaving')


if __name__ == "__main__":
    main()
I believe I was not very clear when expressing my doubts

the program I'm doing is executed and is waiting for user interest, when the user sends a command the program executes this command.

I want to put a function that is sending for example a status for the users, I tried to do by scheduling or thread and I could not, it is only executing the thread or the thread ignoring the commands sent by the users


Note: I do not know if the correct term would be Multiply Processing
You could start a thread a status_function() and keep it alive until the end of the main() execution.
When I try to do it or it just waits for commands or is only sending hourly statuses, I'm not getting it to do both at same time

run.py
class MyClass(object):
    def __init__(self):
        builder = StackBuilder()

        self.stack = builder\
            .pushDefault(encryption)\
            .push(Layer)\
            .build()

        self.stack.setCredentials(credentials)
        self.stack.setProp(CONTACTS,  list(config.keys()))
        self.stack.setProp(IDENTITY_AUTOTRUST, True)

    def start(self):
        print("[Start]\n")

        self.stack.Event(LayerEvent(Network.EVENT_STATE_CONNECT))

        try:
            self.stack.loop(timeout=0.5, discrete=0.5)
        except AuthError as e:
            print("ERROR: %s" % e)
        except KeyboardInterrupt:
            print("\nLeaving")
            sys.exit(0)

def run_infinite():
    while True:
        try:
            c = MyClass()
            c.start()
        except:
            pass
        else:
            break

if __name__ == "__main__":
    c = MyClass()
    c.start()
run.py call my_funcionts.py

my_funcionts.py
def handle(command):
    user = [ "rvalentim",
             "tgusmao",
                ]
    if command.users in user:
        if command.text == "info":
            info(command)
        elif command.text == "test":
            test(message)
        else :
            other(message)

'''
My funcionts module code
==========================================================
'''
def info(command):
    print("Info")

def test(command):
    print("Testing")

def other(command):
    print("Other info")
'''
==========================================================
'''
my_funcionts.py is in stand by waiting for user to send a command, but when I try to implement multiprocessing, where it continues waiting for commands to be received and at same time leave another routine running where it sends hourly status for example
would anyone have any ideas to help me?