很棒的WM-os.execute与afwul.spawn [英] Awesome WM - os.execute vs afwul.spawn

查看:186
本文介绍了很棒的WM-os.execute与afwul.spawn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道lua的

  os.execute('< command>')$ b是否有区别$ b  

和很棒的

  awful.spawn('< command>')

我注意到lain 建议使用 os.execute ,是否有任何原因,或者是个人喜好?

解决方案

请勿使用 os.execute io.popen 。它们阻止了功能,并导致非常差的性能,并显着增加了输入(鼠标/键盘)和客户端重绘延迟。使用这些功能是一种反模式,只会使所有事情变得更糟。



请参阅文档中的警告 https://awesomewm.org/apidoc/libraries/awful.spawn.html



<现在要真正回答问题,您必须了解Awesome是单线程的。这意味着它一次只做一件事情。如果调用 os.execute( sleep 10),鼠标将继续移动,但否则计算机将冻结10秒钟。对于执行得足够快的命令,可能有可能 错过了这一点,但请记住,由于延迟/频率规则,任何需要33毫秒执行的命令在执行时都会掉落2帧。一个60fps的视频游戏(并且至少删除一个)。如果您每秒有很多命令,它们会加起来并破坏您的系统性能。



但是Awesome注定不会很慢。它可能没有多个线程,但是有一个叫做协程的东西,也有回调。这意味着需要花费时间执行的事情仍可以在后台执行(使用C线程或外部进程+套接字)。完成后,他们可以通知Awesome线程。



现在,这将使我们进入下一部分。如果我这样做:

 -**不要**这样做。 
os.execute( sleep 1; echo foo> /tmp/foo.txt)
mylabel.text = io.popen( cat /tmp/foo.txt):read( * all *)

标签将显示 foo 。但是如果我这样做了:

 -假设/tmp/foo.txt不存在
awful.spawn.with_shell ( sleep 1; echo foo> /tmp/foo.txt)
mylabel.text = io.popen( cat /tmp/foo.txt\"):read(\"*all*)

然后标签将为空。 awful.spawn awful.spawn.with_shell 阻止,因此 io.popen 将在 sleep 1 完成之前执行yayy。这就是为什么我们有异步函数来执行shell命令的原因。有许多具有不同特征和复杂性的变体。 awful.spawn.easy_async 是最常见的,因为它对一般的我想执行命令并在完成时对输出执行某些操作足够好。 p>

  awful.spawn.easy_async_with_shell( sleep 1; echo foo> /tmp/foo.txt,function()
awful.spawn.easy_async_with_shell( cat /tmp/foo.txt,function(out)
mylabel.text = out
end)
end)

在此变体中,Awesome不会阻止。同样,像其他衍生工具一样,您不能在回调函数之外添加代码以使用命令的结果。该代码将在命令完成之前执行,因此结果将不可用。



还有一个名为协程的东西可以消除回调的需要,但是目前难以在Awesome中使用,并且也难以解释。


I was wondering if there are any difference between lua's

os.execute('<command>')

and awesome's

awful.spawn('<command>')

I noticed that lain suggests to use os.execute, is there any reason for that, or it's a matter of personal taste?

解决方案

Do not ever use os.execute or io.popen. They are blocking functions and cause very bad performance and noticeable increase in input (mouse/keyboard) and client repaint latency. Using these functions is an anti-pattern and will only make everything worst.

See the warning in the documentation https://awesomewm.org/apidoc/libraries/awful.spawn.html

Now to really answer the question, you have to understand that Awesome is single threaded. It means it only does a single thing at a time. If you call os.execute("sleep 10"), your mouse will keep moving but your computer will otherwise be frozen for 10 seconds. For commands that execute fast enough, it may be possible to miss this, but keep in mind that due to the latency/frequency rules, any command that takes 33 millisecond to execute will drop up to 2 frames in a 60fps video game (and will at least drop one). If you have many commands per second, they add up and wreck your system performance.

But Awesome isn't doomed to be slow. It may not have multiple threads, but it has something called coroutine and also have callbacks. This means things that take time to execute can still do so in the background (using C threads or external process + sockets). When they are done, they can notify the Awesome thread. This works perfectly and avoid blocking Awesome.

Now this brings us to the next part. If I do:

-- Do **NOT** do this.
os.execute("sleep 1; echo foo > /tmp/foo.txt")
mylabel.text = io.popen("cat /tmp/foo.txt"):read("*all*")

The label will display foo. But If I do:

-- Assumes /tmp/foo.txt does not exist
awful.spawn.with_shell("sleep 1; echo foo > /tmp/foo.txt")
mylabel.text = io.popen("cat /tmp/foo.txt"):read("*all*")

Then the label will be empty. awful.spawn and awful.spawn.with_shell will not block and thus the io.popen will be execute wayyy before sleep 1 finishes. This is why we have async functions to execute shell commands. There is many variants with difference characteristics and complexity. awful.spawn.easy_async is the most common as it is good enough for the general "I want to execute a command and do something with the output when it finishes".

awful.spawn.easy_async_with_shell("sleep 1; echo foo > /tmp/foo.txt", function()
    awful.spawn.easy_async_with_shell("cat /tmp/foo.txt", function(out)
        mylabel.text = out
    end)
end)

In this variant, Awesome will not block. Again, like other spawn, you cannot add code outside of the callback function to use the result of the command. The code will be executed before the command is finished so the result wont yet be available.

There is also something called coroutine that remove the need for callbacks, but it is currently hard to use within Awesome and would also be confusing to explain.

这篇关于很棒的WM-os.execute与afwul.spawn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆