计划python中的问题 [英] Scheduling issues in python

查看:77
本文介绍了计划python中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python将硬件usb嗅探器设备与供应商提供的python API接口,并且我试图在无限循环中的单独线程中从该设备读取(usb数据包)(工作正常) .问题在于我的主循环似乎不再被调度(我的读取循环引起了所有人的注意).

I'm using python to interface a hardware usb sniffer device with the python API provided by the vendor and I'm trying to read (usb packets) from the device in a separate thread in an infinite loop (which works fine). The problem is that my main loop does not seem to ever get scheduled again (my read loop gets all the attention).

代码看起来像这样:

from threading import Thread
import time
usb_device = 0

def usb_dump(usb_device):
    while True:
        #time.sleep(0.001)
        packet = ReadUSBDevice(usb_device)
        print "packet pid: %s" % packet.pid

class DumpThread(Thread):
    def run(self):
        usb_dump()

usb_device = OpenUSBDevice()
t = DumpThread()
t.start()
print "Sleep 1"
time.sleep(1)
print "End"
CloseUSBDevice(usb_device)
sys.exit(0)

(我可以粘贴实际的代码,但是由于您需要硬件设备,因此我认为它没有太大帮助).

(I could paste actual code, but since you need the hardware device I figure it won't help much).

我希望这段代码能在主线程终止整个程序之前开始转储usb数据包大约一秒钟.但是,我看到的只是睡眠1",然后usb_dump()过程将永远运行.如果我在usb_dump()过程的内部循环中取消对"time.sleep(0.001)"语句的注释,事情将按照我期望的方式开始工作,但是python代码变得无法跟上所有传入的数据包:-(

I'm expecting this code to start dumping usb packets for about a second before the main thread terminates the entire program. However, all I see is "Sleep 1" and then the usb_dump() procedure runs forever. If I uncomment the "time.sleep(0.001)" statement in the inner loop of the usb_dump() procedure things start working the way I expect, but then the python code becomes unable to keep up with all the packets coming in :-(

供应商告诉我,这是python调度程序问题,而不是其api的错误,因此对我没有帮助:

The vendor tells me that this is an python scheduler problem and not their api's fault and therefor won't help me:

«但是,似乎在Python中使用线程时遇到了一些细微差别.通过将time.sleep放入DumpThread线程中,即可显式向Python线程系统发出信号以放弃控制.否则,将由Python解释器决定何时切换线程,通常是在执行一定数量的字节码指令后才执行.»

«However, it seems like you are experiencing some nuances when using threading in Python. By putting the time.sleep in the DumpThread thread, you are explicitly signaling to the Python threading system to give up control. Otherwise, it is up the Python interpreter to determine when to switch threads and it usually does that after a certain number of byte code instructions have been executed.»

有人可以确认python是这里的问题吗?还有另一种方法可以使DumpThread释放控件吗?还有其他想法吗?

Can somebody confirm that python is the problem here? Is there another way to make the DumpThread release control? Any other ideas?

推荐答案

如果您的供应商提供的是纯python 代码,那么您的供应商将是正确的;但是,C扩展程序可能会发布 GIL ,并且因此可以进行实际的多线程处理.

Your vendor would be right if yours was pure python code; however, C extensions may release the GIL, and therefore allows for actual multithreading.

尤其是time.sleep 发布GIL(您可以直接从源代码

In particular, time.sleep does release the GIL (you can check it directly from the source code, here - look at floatsleep implementation); so your code should not have any problem. As a further proof, I have made also a simple test, just removing the calls to USB, and it actually works as expected:

from threading import Thread
import time
import sys

usb_device = 0

def usb_dump():
    for i in range(100):
        time.sleep(0.001)
        print "dumping usb"

class DumpThread(Thread):
    def run(self):
        usb_dump()

t = DumpThread()
t.start()
print "Sleep 1"
time.sleep(1)
print "End"
sys.exit(0)

最后,关于您发布的代码,请注意以下几点:

Finally, just a couple of notes on the code you posted:

  • usb_device未传递到线程.您需要将其作为参数传递,或(argh!)告诉线程从全局名称空间获取它.
  • 与其强迫sys.exit(),不如只是发出信号通知线程停止,然后关闭USB设备,否则可能会更好.我怀疑您的代码可能会遇到一些多线程问题,就像现在一样.
  • 如果您只需要定期轮询,则threading.Timer类可能是您更好的解决方案.

[ Update ]关于最新点:正如评论中所述,我认为Timer会更好地适合您函数的语义(定期轮询),并且会自动避免出现问题.供应商代码未发布GIL.

[Update] About the latest point: as told in the comment, I think a Timer would better fit the semantic of your function (a periodic poll) and would automatically avoid issues with the GIL not being released by the vendor code.

这篇关于计划python中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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