1

我有一个我希望始终在后台运行的 python 脚本。应用程序所做的是它转到 Oracle 数据库并检查是否有要向用户显示的消息,如果有,则使用 pynotify 显示通知。

我尝试使用 Timer 对象,但它只在选择时间之后调用该方法。我希望它在选择时间之后每次调用。

if __name__ == '__main__':
  applicationName = "WEWE"
    # Initialization of the Notification library
  if not pynotify.init(applicationName):
   sys.exit(1)         

  t = threading.Timer(5.0, runWorks)
  t.start() 

会做这项工作,有没有更好的方法?

 if __name__ == '__main__':
      applicationName = "WEEWRS"
        # Initialization of the Notification library
      if not pynotify.init(applicationName):
       sys.exit(1)         
      while True:
       t = threading.Timer(5.0, runWorks)
       t.start() 

但这给了我另一个问题。

thread.error: can't start new thread

(r.py:12227): GLib-ERROR **: creating thread 'gdbus': Error creating thread: Resource temporarily unavailable
4

2 回答 2

2

我解决了减少字符串创建的问题。以下错误 -

thread.error: can't start new thread

(r.py:12227): GLib-ERROR **: creating thread 'gdbus': Error creating thread: Resource temporarily unavailable

资源不足时出现。以下是更正后的代码。

if __name__ == '__main__':
  applicationName = "DSS POS"
  # Initialization of the Notification library
  if not pynotify.init(applicationName):
   sys.exit(1)         

  flagContinous = True
  timeout = 5
  # This loop will continously keep the application in the background
  while flagContinous:
   time.sleep(timeout)
   runWorks()
  # After 30 seconds, "hello, world" will be printed  

我还使用了一个锁定文件,这样脚本就不会多次运行。

pid = str(os.getpid())
  pidfile = "/tmp/mydaemon.pid"

  # If we have a lock already block the program
  if os.path.isfile(pidfile):
    print "%s already exists, exiting" % pidfile
    sys.exit()
  else:
    file(pidfile, 'w').write(pid)
  # Do all the work
  applicationName = "DSS POS"
  # Initialization of the Notification library
  if not pynotify.init(applicationName):
   sys.exit(1)         

  # Controls for the application
  flagContinous = True
  timeout = 5
  # This loop will continously keep the application in the background
  while flagContinous:
   time.sleep(timeout)
   runWorks()
  # After 30 seconds, "hello, world" will be printed  

  # Release the file
  os.unlink(pidfile)
于 2012-06-13T07:20:39.933 回答
1

使用这个简单的守护程序脚本总是更好:http: //www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

它做得很好!

于 2012-06-13T08:24:28.247 回答