Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python线程化子函数_Python_Multithreading - Fatal编程技术网

Python线程化子函数

Python线程化子函数,python,multithreading,Python,Multithreading,我的应用程序正在循环中运行。。有时需要从回路中调用led闪烁功能。我差不多是这样做的 def led_red_flash(flashcount): logging.debug('Starting') for l in range(0,flashcount): GPIO.output(16,GPIO.HIGH) time.sleep(0.1) GPIO.output(1

我的应用程序正在循环中运行。。有时需要从回路中调用led闪烁功能。我差不多是这样做的

def led_red_flash(flashcount):
        logging.debug('Starting')

        for l in range(0,flashcount):
                GPIO.output(16,GPIO.HIGH)
                time.sleep(0.1)
                GPIO.output(16,GPIO.LOW)
                time.sleep(0.1)
        logging.debug('Stopping')

while True:
       <do some stuff here>
       t = threading.Thread(name='led_red_flash', target=led_red_flash(100) )
       t.start()
while True:
       <do some stuff here>
       led_red_flash(100)
def led_红色_闪烁(闪光计数):
logging.debug('Starting')
对于范围内的l(0,flashcount):
GPIO.输出(16,GPIO.高)
睡眠时间(0.1)
GPIO.输出(16,GPIO.低)
睡眠时间(0.1)
logging.debug('停止')
尽管如此:
t=线程。线程(name='led\u red\u flash',target=led\u red\u flash(100))
t、 开始()
这很有效。。但是有没有一天可以把所有的线程放在def led_red_闪存部分?随着我的脚本变得越来越复杂,阅读起来会越来越困难。像这样的事情

def led_red_flash(flashcount):
        logging.debug('Starting')

        for l in range(0,flashcount):
                GPIO.output(16,GPIO.HIGH)
                time.sleep(0.1)
                GPIO.output(16,GPIO.LOW)
                time.sleep(0.1)
        logging.debug('Stopping')

while True:
       <do some stuff here>
       t = threading.Thread(name='led_red_flash', target=led_red_flash(100) )
       t.start()
while True:
       <do some stuff here>
       led_red_flash(100)
为True时:
led_红色_闪烁(100)

上面是我正在运行的循环的一个非常简化的版本。在实际脚本中,不可能同时运行多个led_red_flash实例。。所以这不是一个问题

您可以创建包装函数:

def _led_red_flash(flashcount):
    logging.debug('Starting')
    for l in range(0,flashcount):
        GPIO.output(16,GPIO.HIGH)
        time.sleep(0.1)
        GPIO.output(16,GPIO.LOW)
        time.sleep(0.1)
    logging.debug('Stopping')


def led_red_flash(flashcount):
    t = threading.Thread(name='led_red_flash', target=_led_red_flash, args=(100,))
    t.start()
    return t
顺便说一句,您的原始代码没有在单独的线程中执行
led\u red\u flash
。我刚刚打电话给led红色闪光灯(
led红色闪光灯(100)

您应该传递函数本身,而不是函数调用的返回值。看


你是在要求设计审查,还是有编程问题?这就是工作。。。我自己也试过一个包装器,但我想它不起作用,因为有争论。谢谢