Python Windows 服务的工作目录是什么?

发布于 2024-11-01 14:57:14 字数 1098 浏览 0 评论 0原文

我已经使用 pywin32 成功创建了一个 Python Windows 服务。在测试我的应用程序时,我尝试将其打印(这没有按我的预期工作),并且还将其写入文件。它能够写入文件,但该文件最终位于 python 库 site-packages 文件夹中。这似乎是工作目录所在的位置,尽管我不确定为什么?我想知道指定工作目录应该是什么的最佳方法。

我可以打开具有完整路径名的文件,或者我可以使用 os.cwd 吗?最佳实践是什么?

这是组成我的 Windows 服务的两个文件。

import os
import sys
import win32service
import win32serviceutil
from twisted.internet import reactor
import xpress


class XPressService(win32serviceutil.ServiceFramework):

    _svc_name_ = 'XPress'
    _svc_display_name_ = 'XPress Longer Name'

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        reactor.callFromThread(reactor.stop)

    def SvcDoRun(self):
        xpress.main()
        reactor.run(installSignalHandlers=False)


if __name__ == "__main__":
    win32serviceutil.HandleCommandLine(XPressService)

下面是由上述脚本导入的“xpress.py”。

import datetime

def main():
    with open('times', 'a') as f:
        print str(datetime.datetime.now())
        f.write(str(datetime.datetime.now()))

if __name__ == '__main__':
    main()

I have successfully created a Python Windows service using pywin32. In testing my application I attempted to have it print (which didn't work as I expected) and I also had it write to a file. It was able to write to a file, but the file ended up in the python library site-packages folder. This appears to be where the working directory is, though I'm not sure why? I would like to know the best way to specify what the working directory should be.

I could open files with full path names, or I could maybe use os.cwd? What is the best practice?

Here are the two files which compose my Windows service.

import os
import sys
import win32service
import win32serviceutil
from twisted.internet import reactor
import xpress


class XPressService(win32serviceutil.ServiceFramework):

    _svc_name_ = 'XPress'
    _svc_display_name_ = 'XPress Longer Name'

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        reactor.callFromThread(reactor.stop)

    def SvcDoRun(self):
        xpress.main()
        reactor.run(installSignalHandlers=False)


if __name__ == "__main__":
    win32serviceutil.HandleCommandLine(XPressService)

Below is "xpress.py" which is imported by the above script.

import datetime

def main():
    with open('times', 'a') as f:
        print str(datetime.datetime.now())
        f.write(str(datetime.datetime.now()))

if __name__ == '__main__':
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じее 2024-11-08 14:57:14

它们都有效,这就是您的需求。由于各种原因,最好使用文件名的绝对路径,这样您就不必担心应用程序在“哪里”工作,您只需知道输出将在哪里(这是最重要的)。在 *nix 中,当应用程序没有指定的工作目录时,它们通常在“/”中工作。如果您确实选择在另一个目录(os.chdir(newDir))中工作,请在调用 win32serviceutil.HandleCommandLine 之前执行此操作

我不知道 Windows 默认值,但您可能将其与站点包中的库目录固定在一起。

They both work, it's what your needs are. For various reasons, it's probably best to use absolute paths to the file names, this way you don't have to worry about 'where' your app is working, you just know where the output will be (which is most important). In *nix, apps generally work in '/' when they don't have a specified working directory. If you do choose to work in another directory it's os.chdir(newDir), do this before you call win32serviceutil.HandleCommandLine

I don't know the windows default, but you probably nailed it with the library's directory in site-packages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文