相关疑难解决方法(0)

快速精确的Python重复计时器

我需要快速准确地从列表中发送重复消息.一个列表需要每100ms发送一次消息,窗口为+/- 10ms.我尝试使用下面的代码,但问题是计时器等待100毫秒,然后所有计算都需要完成,使计时器落在可接受的窗口之外.

简单地减少等待是一个混乱,不可靠的黑客.如果在循环期间编辑列表,则消息循环周围会有一个Lock.

关于如何让python在100ms左右发送消息的想法?谢谢

from threading import Timer
from threading import Lock

class RepeatingTimer(object):
    def __init__(self,interval, function, *args, **kwargs):
        super(RepeatingTimer, self).__init__()
        self.args = args
        self.kwargs = kwargs
        self.function = function
        self.interval = interval
        self.start()

    def start(self):
        self.callback()

    def stop(self):
        self.interval = False

    def callback(self):
        if self.interval:
            self.function(*self.args, **self.kwargs)
            Timer(self.interval, self.callback, ).start()

def loop(messageList):
    listLock.acquire()
    for m in messageList:
        writeFunction(m)
    listLock.release()


MESSAGE_LIST = [] #Imagine this is populated with the messages
listLock = Lock()
rt = RepeatingTimer(0.1,loop,MESSAGE_LIST)
#Do other stuff after …
Run Code Online (Sandbox Code Playgroud)

python multithreading timer repeat

7
推荐指数
2
解决办法
1万
查看次数

为什么time.clock比time.time给出更长的时间?

我使用time.clock和在Ubuntu上定时了一段python代码time.time:

clock elapsed time: 8.770 s
time  elapsed time: 1.869 s
Run Code Online (Sandbox Code Playgroud)

我知道time.time使用系统时间和time.clock使用处理器时钟.当time.time给出比time.clock更长的经过时间时,对我来说很有意义:处理器在整个时间内都没有活动(例如,调用时间time.sleep).

但是为什么/何时处理器时钟会比系统时间大得多


附录

我做了一个粗略的测试,使用标准映射计算相同的函数,使用进程池映射和线程池映射.可以理解,进程池速度更快,线程池更慢.更有趣的是:时钟时序小于处理器池的时间,但线程池中的时间更长.

同样,我理解为什么处理器池的时钟时序较少:假设主进程没有做太多事情,只是等待池进程完成.但是为什么线程池的时钟时间更长?任何见解?

结果:

map
  time  1738.8
  clock 1739.6
mp pool
  time   580.1
  clock   15.9
thread pool
  time  3455.3
  clock 5378.9
Run Code Online (Sandbox Code Playgroud)

码:

from time import clock, sleep, time
from multiprocessing.pool import ThreadPool
from multiprocessing import Pool
import random

def f(i):
    x = [random.random() for j in range(100000)]
    return x[i]

def t(fn):
    t0, c0 = time(), …
Run Code Online (Sandbox Code Playgroud)

python linux time multithreading multiprocessing

6
推荐指数
1
解决办法
1948
查看次数

在没有busywait的情况下在python中实现亚毫秒处理

我将如何使用linux下的python(在单核Raspberry Pi上运行)实现毫秒精度的数组处理.

我正在尝试解析MIDI文件中的信息,该文件已被预处理到一个数组,其中每毫秒我检查数组是否在当前时间戳处有条目并触发某些功能(如果有).

目前我正在使用time.time()并使用繁忙的等待(如此处所述).这会占用所有CPU,因此我选择了更好的解决方案.

# iterate through all milliseconds
for current_ms in xrange(0, last+1):
  start = time()

  # check if events are to be processed
  try:
    events = allEvents[current_ms]
    # iterate over all events for this millisecond
    for event in events:
      # check if event contains note information
      if 'note' in event:
        # check if mapping to pin exists
        if event['note'] in mapping:
          pin = mapping[event['note']]
          # check if event contains on/off information
          if 'mode' in event:
            if …
Run Code Online (Sandbox Code Playgroud)

python linux midi python-2.7 raspberry-pi

5
推荐指数
1
解决办法
1385
查看次数

为什么Mac OS X上的Python time.time()和time.clock()存在差异?

我正在运行Mac OS X 10.8并且对time.clock()有一些奇怪的行为,一些在线消息来源说我应该更喜欢time.time()来为我的代码计时.例如:

import time    
t0clock = time.clock()
t0time = time.time()
time.sleep(5)
t1clock = time.clock()
t1time = time.time()
print t1clock - t0clock
print t1time - t0time

0.00330099999999 <-- from time.clock(), clearly incorrect
5.00392889977    <-- from time.time(), correct
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我应该使用time.time()进行可靠的估算吗?

python macos time

4
推荐指数
2
解决办法
3138
查看次数

Mac与Windows中的Pythons time.clock()有什么区别?

我正在使用Python的时间来衡量Selenium流程的时间范围.我的脚本是这样的......

start_time = time.clock()
...
#ending with
final_time = '{0:.2f}'.format(time.clock()-start_time)
Run Code Online (Sandbox Code Playgroud)

当在Windows操作系统上运行时,我会得到类似的东西55.22但是如果在Mac上运行它会返回类似的东西,.14即使它几乎是同一时间.

知道Mac上发生了什么不同的事情吗?我实际上也会尝试使用Ubuntu以查看差异.

python macos time

1
推荐指数
1
解决办法
443
查看次数