Python/Perl:定时循环实现(也具有微秒级)? [英] Python/Perl: timed loop implementation (also with microseconds)?

查看:126
本文介绍了Python/Perl:定时循环实现(也具有微秒级)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Perl和/或Python来实现以下JavaScript伪代码:

I would like to use Perl and/or Python to implement the following JavaScript pseudocode:

var c=0;
function timedCount()
{
  c=c+1;
  print("c=" + c);

  if (c<10)  {
    var t;
    t=window.setTimeout("timedCount()",100);
  }
}

// main:
timedCount();
print("after timedCount()");

var i=0;
for (i=0; i<5; i++) {
  print("i=" + i);
  wait(500); //wait 500 ms
}

 

现在,这是一个非常不幸的示例可供选择作为基础-但我根本想不出任何其他语言来提供它:)基本上,有一个主循环"和一个辅助循环"( timedCount),两者的计数速率不同:main周期为500 ms(通过wait实现),timedCount周期为100 ms(通过setInterval实现).但是,JavaScript本质上是单线程的,而不是多线程的-因此,没有真正的sleep/wait/pause或类似名称(参见

Now, this is a particularly unlucky example to choose as a basis - but I simply couldn't think of any other language to provide it in :) Basically, there is a 'main loop' and an auxiliary 'loop' (timedCount), which both count at different rates: main with 500 ms period (implemented through a wait), timedCount with 100 ms period (implemented via setInterval). However, JavaScript is essentially single-threaded, not multi-threaded - and so, there is no real sleep/wait/pause or similar (see JavaScript Sleep Function - ozzu.com), which is why the above is, well, pseudocode ;)

但是,通过将主要部分移至另一个setInterval函数,我们可以获得可以粘贴并在诸如 EnvJS/Rhino ):

By moving the main part to yet another setInterval function, however, we can get a version of the code which can be pasted and ran in a browser shell like JavaScript Shell 1.4 (but not in a terminal shell like EnvJS/Rhino):

var c=0;
var i=0;
function timedCount()
{
  c=c+1;
  print("c=" + c);

  if (c<10)  {
    var t;
    t=window.setTimeout("timedCount()",100);
  }
}

function mainCount() // 'main' loop
{
  i=i+1;
  print("i=" + i);

  if (i<5)  {
    var t;
    t=window.setTimeout("mainCount()",500);
  }
}

// main:
mainCount();
timedCount();
print("after timedCount()");

...结果类似于以下输出:

... which results with something like this output:

i=1
c=1
after timedCount()
c=2
c=3
c=4
c=5
c=6
i=2
c=7
c=8
c=9
c=10
i=3
i=4
i=5

...即主要计数和辅助计数是交错的"/线程化的"/散布的",主计数按预期大约每五个辅助计数计算一次.

... that is, the main counts and auxiliary counts are 'interleaved'/'threaded'/'interspersed', with a main count on approx every five auxiliary counts, as anticipated.

 

现在是主要问题-分别在Perl和Python中建议这样做的推荐方式是什么?

And now the main question - what is the recommended way of doing this in Perl and Python, respectively?

  • 另外,Python或Perl是否提供了以跨平台方式以微秒计时分辨率实现上述功能的工具?

 

非常感谢您的回答,
干杯!

Many thanks for any answers,
Cheers!

推荐答案

在Python中我想到的最简单,最通用的方法是使用

The simplest and most general way I can think of doing this in Python is to use Twisted (an event-based networking engine) to do this.

from twisted.internet import reactor
from twisted.internet import task

c, i = 0, 0
def timedCount():
    global c
    c += 1
    print 'c =', c

def mainCount():
    global i
    i += 1
    print 'i =', i

c_loop = task.LoopingCall(timedCount)
i_loop = task.LoopingCall(mainCount)
c_loop.start(0.1)
i_loop.start(0.5)
reactor.run()

Twisted具有称为反应器的高效且稳定的事件循环实现.这使得它成为单线程的,并且在本质上类似于上述示例中的Javascript.我之所以用它来执行上述定期任务之类的原因是,它提供了使您可以轻松添加任意多个复杂期间的工具.

Twisted has a highly efficient and stable event-loop implementation called the reactor. This makes it single-threaded and essentially a close analogue to Javascript in your example above. The reason I'd use it to do something like your periodic tasks above is that it gives tools to make it easy to add as many complicated periods as you like.

它还提供了更多计划任务调用的工具有趣.

这篇关于Python/Perl:定时循环实现(也具有微秒级)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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