简体   繁体   English

python 程序作为 systemd 运行比从终端运行慢

[英]python program slower running as a systemd than running it from terminal

I have raspberri pi zero WH which has just 1 Core .我有raspberri pi 零 WH ,它只有1 个 Core

I have a program that runs in a loop, listens to incoming UDP packets and process them.我有一个循环运行的程序,监听传入的 UDP 数据包并处理它们。 It is crucial for me, that the packets and further logic is processed in 0.025s (to be more clear, there is eternal loop(even if no packets are coming) and whatever happens, the loop cannot exceed 0.025s).对我来说至关重要的是,数据包和进一步的逻辑在 0.025 秒内处理(更清楚的是,存在永恒循环(即使没有数据包到来),无论发生什么,循环都不能超过 0.025 秒)。

I am fairly succesful with this if I run my program from terminal as如果我从终端运行我的程序,我会相当成功

sudo python3 script.py

However, if I run it with systemd as a service , it seems that the program is not taking enough resources.但是,如果我使用systemd作为服务运行它,程序似乎没有占用足够的资源。 Loops are failing the 0.025s condition.循环未达到 0.025s 条件。 Also it seems from htop, that running my program as a service makes one thread less (I cant guarantee this fact, just quick observation).此外,从 htop 看来,将我的程序作为服务运行会减少一个线程(我不能保证这一事实,只是快速观察)。

I dont have experience with systemd services , so maybe the problem is with my configuration我没有使用systemd 服务的经验,所以问题可能出在我的配置上

[Unit]
Description=App

[Service]
ExecStart=sudo /usr/bin/python3 /home/pi/script.py
WorkingDirectory=/home/pi/
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

My question is, is this standard behaviour or systemd service should behave same as program runned from terminal?我的问题是,这个标准行为或 systemd 服务是否应该与从终端运行的程序一样?

It is entirely possible that scripts running as systemd service may be slower than directly from Terminal - likely due to different resource allocations. 作为systemd服务运行的脚本完全可能比直接从终端运行慢 - 可能是由于不同的资源分配。 For instance, I would expect scripts ran from Terminal (ie explicitly by the user) to receive a higher priority and therefore greater amount of resources in contrast to background processing. 例如,我希望脚本从终端(即用户明确地)运行以获得更高的优先级,因此与后台处理相比,资源量更大。

You can, however, assign more resources including CPU and RAM to your service using systemd.resource-control . 但是,您可以使用systemd.resource-control为服务分配更多资源,包括CPU和RAM。

For example, you could try increasing CPUWeight from the default value of 100 to 1,000. 例如,您可以尝试将CPUWeight从默认值100增加到1,000。

[Service]
CPUWeight = 1000

Adjusting other parameters may finally lead to your desired execution time. 调整其他参数可能最终导致您所需的执行时间。 Do keep in mind that providing a greater amount of resources to your service it may impact the performance of your overall system. 请记住,为您的服务提供更多资源可能会影响整个系统的性能。

I had a similar issue and solved this by creating a "user.service".我遇到了类似的问题并通过创建“user.service”解决了这个问题。 Save the unit file in:将单元文件保存在:

/home/$USER/.config/system/user/<xyz>.service

then start it via:然后通过以下方式启动它:

systemctl --user start xyz

The performance is then the same as if you were running it via a logged in shell.然后,性能与您通过登录 shell 运行它一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM