How to run a python code in background? [ANSWERED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to run a python code in background? [ANSWERED]

I have made a python program to notify about news etc. after every hour but I'm unable to run it in the background 24*7. I've tried the 'pythonw' command but it isn't running.

12th Jan 2024, 9:15 AM
Jitesh Singh
Jitesh Singh - avatar
27 Answers
+ 3
try this to debug: pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt can you post your script?
12th Jan 2024, 9:31 AM
Bob_Li
Bob_Li - avatar
+ 3
Abdurahman Jemal Adem maybe Jitesh Singh can help explain what the main goal of the topic is. It is not actually about ui and popups, but how to make a python program run invisibly in your computer or device. You cannot do this in Sololearn.
13th Jan 2024, 1:26 PM
Bob_Li
Bob_Li - avatar
+ 2
oh. I thought it was a lieral gui popup 😁. so i created them with pysimplegui. Anyway, the main goal is scheduled background script execution. The executed task can actually be anything.
13th Jan 2024, 12:31 PM
Bob_Li
Bob_Li - avatar
+ 2
JaScript yes, async operations are probably better for more complex things
14th Jan 2024, 7:40 AM
Bob_Li
Bob_Li - avatar
+ 1
You can use this application, Sololearn, for your own code. Just click on the 'Create', then choose the language of your code and write down your code, and hit run.
12th Jan 2024, 8:49 PM
Abdurahman Jemal Adem
Abdurahman Jemal Adem - avatar
+ 1
It is a program to notify, it just make a notification pop on the screen.
13th Jan 2024, 7:06 AM
Jitesh Singh
Jitesh Singh - avatar
+ 1
I assume your script is running in a continuous loop? Are you sure it is not killed when the popup is closed? How about network connection? is it being automatically stopped for some other reason?
13th Jan 2024, 7:10 AM
Bob_Li
Bob_Li - avatar
+ 1
Yeah but , using time module, it sleeps for 1 hr than notifies again.
13th Jan 2024, 7:12 AM
Jitesh Singh
Jitesh Singh - avatar
+ 1
Looking at stackoverflow suggestions, maybe use the sched module for scheduling. do a trial with smaller time interval first for testing. https://stackoverflow.com/questions/59125493/how-to-constantly-run-JUMP_LINK__&&__python__&&__JUMP_LINK-script-in-the-background-on-windows
13th Jan 2024, 7:23 AM
Bob_Li
Bob_Li - avatar
+ 1
Jitesh Singh it is an interesting idea. Here is my test. The code will not work in Sololearn, but I got it working as a hidden background process in my laptop. https://sololearn.com/compiler-playground/cDFvvNI3QHD9/?ref=app
13th Jan 2024, 10:57 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li There is no module with the name of "PySimpleGUI".
13th Jan 2024, 11:05 AM
Abdurahman Jemal Adem
Abdurahman Jemal Adem - avatar
+ 1
Abdurahman Jemal Adem you can't run it here in Sololearn you have to run it in a laptop or pc and also pip install the necessary modules. Sololearn is just a learning platform. There are so many things you can do with an actual development setup.
13th Jan 2024, 11:10 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li I have seen that. On here, the error it raises is no module with that name.
13th Jan 2024, 11:16 AM
Abdurahman Jemal Adem
Abdurahman Jemal Adem - avatar
+ 1
Abdurahman Jemal Adem It has a popup gui, so it cannot be executed in Sololearn. Also, scheduled function execution is also probably impossible in Sololearn, since the playground have time limits...
13th Jan 2024, 11:17 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li My code is very much similar to yours except the fact that I have used plyer module instead of PySimpleGui but your code is wonderful.
13th Jan 2024, 12:04 PM
Jitesh Singh
Jitesh Singh - avatar
+ 1
Jitesh Singh pysimplegui is simpler. I will check out the plyer module. plyer looks nice, mobile-centric. I have not played with the Kivy ecosystem for a long time. But I can't find any popup gui examples... maybe you mean Kivy Popup?
13th Jan 2024, 12:05 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li I think I didn't make you understand it very well. By a popup notification, I meant a simple alert notification appearing in the side of the screen.
13th Jan 2024, 12:27 PM
Jitesh Singh
Jitesh Singh - avatar
+ 1
Bob_Li OK 👍 thanks
13th Jan 2024, 1:36 PM
Abdurahman Jemal Adem
Abdurahman Jemal Adem - avatar
+ 1
As Bob_Li said, the main goal was to run a python program as a background task and was not related to the source code.
13th Jan 2024, 1:47 PM
Jitesh Singh
Jitesh Singh - avatar