Is there any way to prevent Windows from shutting down my system with Python. So I can't shutdown my system while my Python script is running?
yes there is.
from windows docs
Shutdown Notifications
Applications with a window and message queue
receive shutdown notifications through the WM_QUERYENDSESSION and
WM_ENDSESSION messages. These applications should return TRUE to
indicate that they can be terminated. Applications should not block
system shutdown unless it is absolutely necessary. Applications should
perform any required cleanup while processing WM_ENDSESSION.
Applications that have unsaved data could save the data to a temporary
location and restore it the next time the application starts. It is
recommended that applications save their data and state frequently;
for example, automatically save data between save operations initiated
by the user to reduce the amount of data to be saved at shutdown.
Console applications receive shutdown notifications in their handler
routines. To register a console handler, use the SetConsoleCtrlHandler
function. Service applications receive shutdown notifications in their
handler routines. To register a service control handler, use the
RegisterServiceCtrlHandlerEx function.
Blocking Shutdown
If an application must block a potential system shutdown, it can call the
ShutdownBlockReasonCreate function. The caller provides a reason
string that will be displayed to the user. The reason string should be
short and clear, providing the user with the information necessary to
decide whether to continue shutting down the system.
so you have to intercept the WM_QUERYENDSESSION message and return False .
and doing some web search i found a discussion about that how to intercept the WM_QUERYENDSESSION in python :
if you've built an app with a message loop, you can receive the
WM_QUERYENDSESSION message. If you want to have a GUI, most GUI
libraries will probably wrap this up in their own way. If you don't
need a GUI, your simplest solution is probably to use PyWin32.
Somewhere in the docs there's a tutorial on creating a hidden window
and writing a simple message loop. Just do that on the main thread,
and do your real work on a background thread, and signal your
background thread when a WM_QUERYENDSESSION message comes in.
Or, much more simply, just use SetConsoleCtrlHandler (again through
PyWin32). This can also catch ^C, ^BREAK, and the user closing your
console, as well as the logoff and shutdown messages that
WM_QUERYENDSESSION catches. More importantly, it doesn't require a
message loop, so if you don't have any other need for one, it's a lot
simpler.
i can't help with code snippet as i don't have access to windows pc and i don't want to share code without testing, but i hope i pointed you to what you should look for.
Related
I have a Minecraft bedrock edition server running on our shared pc. I would like to interface with it via python. However, one problem I have is that my brothers sometimes restart our pc, or Windows updates. I need to know how to detect that shutdown event and send the shutdown command to the server before restart. I am using the subprocess library.
So, what you will need is the win32API and the function described here. You can use this function to add what's called a Control Handler Method that will run whenever the program is being shut down or terminated for any reason, including shutdown. You can find a list of the different codes that can be passed to the handler and their meanings here. Ideally, you should have a handler method that just shuts down the server, waits for it to finish shutting down, and then return.
I don't have any personal experience with the library, but it should be fairly straightforward.
EDIT: as noted by #ErykSun, you will need to create a hidden window in order to receive the events. To be quite honest I'm not sure how to create that hidden window. Some documentation suggests that running your application as a service may also work. I will look into this more if I get time.
I am developing a Python application and I open ports using uPnP. The problem is that when the application is forced to be closed by process, the port is still opened. So I'm searching for an event in PyQt (or something else) to do some actions when the process is closing. Be sure that I'm not talking about the close button.
QApplications aboutToQuit signal appears to be built exactly for this purpose. Simply add a slot to your code and connect to this signal. Your slot should be notified before the application quits.
From the pyQT docs:
This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session.
The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state.
(emphasis mine)
hello to you all :)
i have a program that have a n number of threads(could be a lot) and they do a pretty extensive job. My problem is that sometimes some people turn off or reboot the server(the program runs all day in the company servers) i know that there is a way to make a handler for the linux signals i want to know what i should do to interact with all threads making them to use run a function and then stop working. There is a way to do that?
sorry the bad english :P
The best way of handling this is not requiring any shutdown actions at all.
For example, your signal handler for (e.g.) SIGTERM or SIGQUIT can just call _exit and quit the process with no clean-up.
Under Linux (with non-ancient threads) when one thread calls _exit (or exit if you really want) other threads get stopped too - whatever they were in the middle of doing.
This would be good as it implements a crash-only design.
Crash-only design for a server is based on the principle that the machine may crash at any point, so you need to be able to recover from such a failure anyway, so just make it the normal way of quitting. No extra code should be required as your server should be robust enough anyway.
About the only thing you can do is set a global variable from your signal handler, and have your threads check its value periodically.
As others have already mentioned, signal handlers can get messy (due to the restrictions, particularly in multi-threaded programs), so it's better to chose another option:
have a dedicated thread for handling signals via sigwaitinfo - the bad news, though, is that python doesn't appear to support that out of the box.
use the Linux-specific signalfd to handle signals (either in a separate thread or integrated into some event loop) - at least there is a python-signalfd module you can use.
As there is no need to install signal handlers here, there is no restriction on what you can do when you are notified of a signal and it should be easy to shut down the others threads in your program cleanly.
I know that shutdown -a will abort a Windows shutdown, but I need to know if there is anything any where I can check for to see if a shutdown is in progress.
Ideally, I'd like a small program like this:
import os
while True:
shuttingDown = <shutdown variable to check>
if shuttingDown:
os.system("shutdown.exe -a")
For preventing a Windows shutdown when it is happening, you can react to the WM_QUERYENDSESSION message (don't know if you can do that easily with Python's win32 API but it's simple in C). This might not prevent applications from closing because Windows sends WM_ENDSESSION to those that answer TRUE to the query message.
I guess you rather want to prevent a timed shutdown using "shutdown.exe". I'm sure that program uses InitiateSystemShutdown to show the shutdown dialog, but there are no resources on intercepting that call (at least I didn't find any or know of a Windows feature that allows such a thing).
I'm using Carrot for a message queue in a Django project and followed the tutorial, and it works fine. But the example runs in the console, and I'm wondering how I apply this in Django. The publisher class I'm calling from one of my models in models.py, so that's OK. But I have no idea where to put the consumer class.
Since it just sits there with .wait(), I don't know at what point or where I need to instantiate it so that it's always running and listening for messages!
Thanks!
The consumer is simply a long running script in the example you cite from the tutorial. It pops a message from the queue, does something, then calls wait and essentially goes to sleep until another message comes in.
This script could just be running at the console under your account or configured as a unix daemon or a win32 service. In production, you'd want to make sure that if it dies, it can be restarted, etc (a daemon or service would be more appropriate here).
Or you could take out the wait call and run it under the windows scheduler or as a cron job. So it processes the queue every n minutes or something and exits. It really depends on your application requirements, how fast your queue is filling up, etc.
Does that make sense or have I totally missed what you were asking?
If what you are doing is processing tasks, please check out celery: http://github.com/ask/celery/