I'm developing a script in PyScripter. When I run it in PyScripter it runs fairly well.
However, the script contains two separate threads (one Thread object, and the main flow of the script). When I run the script from the Command prompt it gets stuck in the Thread. It gives no impression of executeing the main process, and it never ends, which it does when I run it inside PyScripter. What should I do?
In your code, use timeout in join() to put time constrain on the thread. For instance
....
yourThread = threading.Thread()
yourThread.start()
yourThread.join(10.0)
....
Instructions of multithreading checks here. Hope it helps you.
Related
I am trying to set up a python code to be executed automatically.
I started with a small code to be executed:
import datetime
with open("out.txt","a") as f:
f.write(datetime.datetime.now().isoformat())
The task will start allright, and executes (The file is modified), but it never ends in the task scheduler.
this and this exist in SO, but have no real answer. The only workaround proposed in these threads is to force the end of task after a given time in Windows, but this requires to know how long the python script will take which will not be the case for my actual task.
How can the task scheduler know that a python script is finished ?
I run it the following way in the task scheduler :
program : cmd
arguments : /c C:\python27\python.exe C:\path\of\script.py
execute in : C:\path\of\
I tried some variations around this, like executing python instead of cmd, but it didn't change anything. I had hoped the /c would force the task to close.
as Gaurav Pundir mentioned, adding sys.exit(0) should end the script properly and thus the task. However, you do need to add the sys library with import sys in order to use sys.exit(0). Hope this helps!
it looks like a bug to me.
Try looking up for python console under Task Manager.
if it is not there then the program has exited successfully.
I have the same issue with Windows 10, python script ran successfully, there is no python console under Task Manager, yet the scheduled task Status still says 'Running'
There seems to be no correct fix for such issue with CMD as the intermediate launcher.
There is a [End] command in Task Scheduler GUI, clicking it will always terminate the CMD/batch file leaving the spawned python.exe process to straw.
The real problem: there doesn't seem to be any way for cmd to pass on the terminate signal to python.exe.. and neither can taskengine reliably determine if python.exe is alive or not.
I ran into the same problem, the python file didn't stop in the task scheduler. I imported sys and wrote sys.exit(0) but I still got the same problem.
Finally, I decided to press "Update" which solved my problem; the status of the task was "Ready", and not "Running". For information, I use windows 11.
I need to create a python script that monitors another python script, specifically, some variables from it. I was thinking of creating an independent thread in the "observer script" that does this. This thread must run until the "executer script" finishes. Maybe all of this can be done inside one big script (observer+executer), I really do not know at the moment.
Is it possible at all to do this with python in windows?
My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?
read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())
The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.
Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.
professionals
I know how to launch a command in Linux's terminal via process, sth likes following:
import subprocess
subprocess.Popen('ifconfig -a')
But this is opened in process, how can I launch that in a thread instead?
I know "thread.start_new_thread", while this should call a function. Within the function, I still have to use subprocess. And this just to open a process again..
Thank you for your help.
Respectfully..
A command like ifconfig always runs in a separate process. There is no way to run that command within only a "thread" of your application.
Perhaps you could provide more detail about why you believe this is necessary, and we may be able to suggest a different approach. For example, if you need to capture the output of the ifconfig command, there are certainly ways of doing that within Python.
As you are calling another process outside of your Python application, I think that there is no solution to make it run inside the Python interpreter.
I have some small python 2.6 scripts built....
Now, I would like run them as seperate processes within a python shell. Each as a seperate process. If one fails to run maybe with its timer, I would like others to continue without killing all scripts.
Should I do this as singleton gui's or combine them into bigger launch pad. My perference would be launch pad type gui....Any ideas?
Its seems that launching scripts out of SciTE, works ok.
Check joblaunch, a shell tool I made for executing interdependent jobs in parallel locally. It has more options.