D
D
Dead Sea2017-06-01 06:28:16
linux
Dead Sea, 2017-06-01 06:28:16

Should you use Python when you can get by with bash?

I'm not used to (dislike) the bash syntax, and I decided to administer Linux using Python 3 scripts. Right now I'm writing a wrapper over the dzen2 API, lemonbar, in order to quickly create convenient panels. Often (not only in this task) you need to use loops that will work in the background constantly checking conditions and updating information. That's just the usual loop on bash eats 0.0 - 0.7 CPU and 0.3 MEM. Less than a percent of resources! And the Python loop crushes the entire CPU of one of the cores. Actually, I wanted to know knowledgeable people, so as not to load the system, the only way out is to master bash? But in Linux, python software is very common, the same pypanel, and I have one empty loop that immediately eats the system. Maybe I don't know about some python features in this area?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2017-06-01
@Dead_Sea

It's hard to say anything specific without code.
There is a suspicion that you are not inserting any delays into the loop. But for most practical tasks, you don’t need to check something every nanosecond, you can do it, for example, once every 100 milliseconds. And for some tasks, even one check per minute can be quite an adequate frequency.
This code is loading my processor by 99-100%

while True:
    x = range(3)

But with this, I almost do not see the python3 process at all in the output of the top command. It appears there only a few times a minute with some ridiculous figure like 0.2% of the processor load.
import time

while True:
    x = range(3)
    time.sleep(0.1)

So use delays in the loop and you will be happy.

C
CityCat4, 2017-06-01
@CityCat4

Learn /bin/sh
And that's why
On /bin/sh you can implement most of the admin tasks by using python (although I'm using Perl - I like it more) only when it's really necessary, when, say, the implementation on /bin/sh becomes cumbersome and incomprehensible to you in a month ...

A
Anatoly Scherbakov, 2017-06-01
@Altaisoft

Maybe you can look at other shells? There have been vague rumors that the various zsh and csh are more logical than bash.

V
viiy, 2017-06-04
@viiy

The strength of bash is not in the syntax, but in close work with various utilities
It is impossible in python to quickly and elegantly do what they call "one-liners" in bash times than python. Sed, grep, tr, cut will do whatever you want with the text and will also be faster than a Python regexp. And all this is everywhere, on linux, os x, solaris, freebsd already out of the box
In general - learn bash

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question