- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
好吧,这大概是一个困难的问题,我有一个 pyGTK 应用程序,它由于我无法捕获/控制的 X Window 错误而随机崩溃。
所以我创建了一个包装器,一旦检测到崩溃就会重新启动应用程序,现在问题来了,当用户注销或关闭系统时,应用程序以状态 1 退出。但是在一些 X 错误上它确实如此也是。
所以我几乎尝试了任何方法来捕捉关机/注销,但没有成功,这是我尝试过的:
import pygtk
import gtk
import sys
class Test(gtk.Window):
def delete_event(self, widget, event, data=None):
open("delete_event", "wb")
def destroy_event(self, widget, data=None):
open("destroy_event", "wb")
def destroy_event2(self, widget, event, data=None):
open("destroy_event2", "wb")
def __init__(self):
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
self.show()
self.connect("delete_event", self.delete_event)
self.connect("destroy", self.destroy_event)
self.connect("destroy-event", self.destroy_event2)
def foo():
open("add_event", "wb")
def ex():
open("sys_event", "wb")
from signal import *
def clean(sig):
f = open("sig_event", "wb")
f.write(str(sig))
f.close()
exit(0)
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
signal(sig, lambda *args: clean(sig))
def at():
open("at_event", "wb")
import atexit
atexit.register(at)
f = Test()
sys.exitfunc = ex
gtk.quit_add(gtk.main_level(), foo)
gtk.main()
open("exit_event", "wb")
这些都没有成功,是否有任何低级别的方法来检测系统关闭? Google 没有找到与此相关的任何内容。
我想一定有办法,对吗? :/
编辑:好的,更多内容。
我创建了这个 shell 脚本:
#!/bin/bash
trap test_term TERM
trap test_hup HUP
test_term(){
echo "teeeeeeeeeerm" >~/Desktop/term.info
exit 0
}
test_hup(){
echo "huuuuuuuuuuup" >~/Desktop/hup.info
exit 1
}
while [ true ]
do
echo "idle..."
sleep 2
done
并且还创建了一个 .desktop 文件来运行它:
[Desktop Entry]
Name=Kittens
GenericName=Kittens
Comment=Kitten Script
Exec=kittens
StartupNotify=true
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Network;GTK;
Name[de_DE]=Kittens
通常这应该在注销时创建 term 文件,在使用 & 启动时创建 hup 文件。但不在我的系统上。 GDM 根本不关心脚本,当我重新登录时,它仍在运行。
我也尝试过使用 shopt -s huponexit
,但没有成功。
编辑 2:
这里还有一些关于真实代码的更多信息,整个事情看起来像这样:
Wrapper Script, that catches errors and restarts the programm
-> Main Programm with GTK Mainloop
-> Background Updater Thread
流程是这样的:
Start Wrapper
-> enter restart loop
while restarts < max:
-> start program
-> check return code
-> write error to file or exit the wrapper on 0
现在关闭时,start program
返回 1。这意味着要么它执行了 hanup,要么父进程终止,主要问题是弄清楚这两者中哪一个刚刚发生。X 错误也会导致 1。捕获 shellscript 是行不通的。
如果您想查看实际代码,请在 GitHub 上查看:
http://github.com/BonsaiDen/Atarashii
最佳答案
好的,我终于找到了解决方案:)
在这种情况下,您根本不能依赖信号。您必须连接到桌面 session 才能收到注销即将发生的通知。
import gnome.ui
gnome.program_init('Program', self.version) # This is going to trigger a warning that program name has been set twice, you can ignore this, it seems to be a problem with a recent version of glib, the warning is all over the place out there
client = gnome.ui.master_client() # connect us to gnome session manager, we need to init the program before this
client.connect('save-yourself', self.on_logout) # This gets called when the user confirms the logout/shutdown
client.connect('shutdown-cancelled', self.on_logout_cancel) # This gets called when the logout/shutdown is canceled
client.connect('die', self.on_logout) # Don't know when this gets called it never got in my tests
def on_logout(self, *args):
# save settings an create a file that tells the wrapper that we have exited correctly!
# we'll still return with status code 1, but that's just gtk crashing somehow
def on_logout_cancel(self, *args):
# simply delete the logout file if it exists
这里有一个重要的注意事项:不要试图在 on_logout
中退出程序,如果这样做,GNOME 将不会识别出您的程序已经退出,并且会给您一些对话框程序仍在运行。
关于python - 在 Linux 下的 Python/GTK 中检测用户注销/关机 - 未收到 SIGTERM/HUP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2490166/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!