Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How can I schedule a later task in Python?

Parent

How can I schedule a later task in Python?

+5
−0

I want my CLI Python program to schedule a task, and then exit. After some times has passed (say 10 minutes) the task should execute.

The task can be a Python method or a shell command, whatever is easier. I can convert my use case to accommodate it.

This would be on Linux only.

How can I schedule a future task from Python?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+0
−0

While the other answers rely on external programs, you can do the same inside python.

Using time.sleep() If you don't need to do any other tasks during this 10 min.

#!/usr/bin/env python3

import time

def doThisIn10min():
  # Replace this with the function you need to do in 10min
  # Or call a other program with subprocess
  print("doThisIn10min() executed")


#do something here

#We are done, lets execute doThisIn10min() in 10 min
time.sleep(10*60)
doThisIn10min()

If you need to do other tasks, you can spawn a second thread.

#!/usr/bin/env python3

from threading import Thread
import time

def taskWeDoIn10min():
  print("taskWeDoIn10min() started")
  time.sleep(10*60) #wait 10 min
  # Insert here whit the code you want to execute in 10min
  print("taskWeDoIn10min() executed")

def doThisInTheMeantime():
  # Replace this with the code you want to execute in the meantime
  for i in range(50):
    time.sleep(10)
    print("inside doThisInTheMeantime()")

t = Thread( target=taskWeDoIn10min )
t.start() #Background task to sleep 10 min and then execute <something> 
doThisInTheMeantime() #In the mean time, we can do other things
t.join() #Wait till the background task ended
print("All ended")

Yet a other method is to use the sched module.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Specific requirements (2 comments)
Specific requirements
Karl Knechtel‭ wrote 8 months ago

OP seems to have clearly intended that the Python program should exit after scheduling the next task, but this is a useful technique for other situations. Actually, I was hoping to prepare a more general Q&A for language-agnostic techniques for this kind of scheduling.

H_H‭ wrote 8 months ago

I don't see the benefit of exiting the python interpreter. He said

The task can be a Python method

So for me it clearly seems to be a reasonable suggestion to do it all in python. Of course not the only way to solve it, but one that works independently of the OS or OS-agnostic.

As far as i understand, he needs to do something in python and then something else in 10min, that can be done in python or a standalone program. Why shouldn't it use the same python instance? I think it is the by far easiest solution.