1

I have some difficulties into making this function running in the background without blocking my entire program, how can I run that function in loop, without blocking my entire program? This is the function: while True: schedule.run_pending()

Thank you for any reply.

Edit:

def FunctioninLoop():
    while True:
        schedule.run_pending()

async def MyFunction():
    ScheduleToexecute=schedule.every().minute.do(Functionscheduled)
    t = Thread(target=FunctioninLoop())
    t.start()
    print("The execution is going on")
1
  • 1
    Have you looked into multiprocessing? Commented Mar 20, 2020 at 17:20

3 Answers 3

3

Threads are what you are looking for. Consider the following code:

from threading import Thread

def myfunc(a, b, c):
    pass

# Creates a thread
t = Thread(target=myfunc, args=(1, 2, 3))

# Launches the thread (while not blocking the main execution)
t.start()

somecode
somecode
somecode

# Waits for the thread to return (not a must)
t.join()

Hope I've helped! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but after the start my program is still waiting for the loop to end
1
import threading
pender = threading.thread(schedule.run_pending) # Does not Block
print("life goes on until...")
pender.join()                 # Blocks until schedule.run_pending() is complete. 

1 Comment

Thank you, but I don't want to block the execution till the operation is complete since I need it to runs all the time.
1

You can use python's subprocess module

https://docs.python.org/3.2/library/subprocess.html

import os

def myfunction():
    ..........


 os.spawnl(os.P_NOWAIT, myfunction())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.