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.
Related
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.
I have an interesting task ahead of me. I need to connect to 45 different websocket streams and stuff them all into the correct places. Sometimes those streams stop working and need to be relaunched. I'm right now running process via bash for each python file to ensure nothing crashes, but I'd rather have a single "server" if you will managing the whole process.
The goal is to eventually have a dashboard of sorts showing the status of each named subprocess, and some stats. I'd like to be able, via dashboard, to relaunch or just stop any of the processes without killing the server, but being able to relaunch those processed at any time.
Is this possible? Any direction?
Most python websockets examples are server/client. I'm just looking for an indefinite client with a relaunch on error w/o ever killing off the server.
I think, Supervisor is what you are looking for.
It has no web dashboard "out of the box", but it has some plugins that implement such feature though.
So I wrote a little multithreaded SMTP program. The problem is every time I run it, it freezes the computer shortly after. The script appears to still work, as my network card is still lighting up and the emails are received, but in some cases it will lock up completely and stop sending the emails.
Here's a link to my two script files. The first is the one used to launch the program:
readFile.py
newEmail.py
First, you're using popen which creates subprocesses, ie. processes not threads. I'll assume this is what you meant.
My guess would be that the program gets stuck in a loop where it generates processes continuously, which the OS will probably dislike. (That kind of thing is known as a forkbomb which is a good way to freeze Linux unless a process limit has been set with ulimit.) I couldn't find the bug though, but if I were you, I'd log messages each time I spawn or kill a subprocess, and if everything is normal, watch the system closely (ps or top on Unix systems) to see if the processes are really being killed.
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/