Python Forum
Schedule with other processing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Schedule with other processing
#1
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
Reply
#2
Have you tried using Threads?
Python3 Threading
Reply
#3
not yet, would you have an example of how you would run a function every 10 minutes?
Reply
#4
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()
Reply
#5
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
Reply
#6
You could start a thread a status_function() and keep it alive until the end of the main() execution.
Reply
#7
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
Reply
#8
would anyone have any ideas to help me?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cancelling previous schedule cosmarchy 2 2,781 Sep-15-2021, 04:55 PM
Last Post: Larz60+
  Hosting a script on a VPS, and on a schedule? oldguy 0 2,942 Mar-13-2021, 02:46 AM
Last Post: oldguy
  something i noticed about the "schedule" module Stan2292 1 1,765 Aug-30-2019, 06:04 AM
Last Post: buran
  How to schedule Jupyter Notebooks with Papermill wendysling 0 2,499 Jun-11-2019, 05:53 PM
Last Post: wendysling
  schedule module conundrum wgovideo 11 4,314 May-29-2019, 06:39 PM
Last Post: wgovideo
  I got a problem with python schedule darktitan 2 3,480 Sep-22-2018, 12:49 PM
Last Post: darktitan
  How to convert Schedule to APScheduler module? penguin9 2 3,761 May-03-2018, 12:44 PM
Last Post: penguin9
  Creating a schedule program ndplokkaar 4 3,946 Nov-23-2017, 04:21 PM
Last Post: ndplokkaar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020