简体   繁体   English

多线程不起作用

[英]multithreading not working

@KSFT @KSFT

currently frustrated by my inability to either decipher the formatting on stackoverflow OR make a simple python script work... 目前由于我无法解密stackoverflow上的格式或使简单的python脚本正常工作而感到沮丧...

whats wrong here? 这怎么了

the program prompts for input to determine the value of dwell but it does not result in an led turning on. 程序会提示您输入以确定停顿值,但不会导致LED亮起。

import threading
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)
frequency = 0.05
dwell = 0.01


def get_input():
    while True:
        dwell=raw_input("Brightness: ")

input_thread=threading.Thread(target=get_input())
input_thread.start()

while True:
    time.sleep(frequency)
        GPIO.output(7, 1)
        time.sleep(dwell)
        GPIO.output(7, 0)
input_thread=threading.Thread(target=get_input())   

is wrong! 是错的!

input_thread=threading.Thread(target=get_input) 

is right! 是对的!

threading 穿线

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

If you want to give arg to get_input ,you need to give it throw args and kwargs. 如果要将arg赋予get_input,则需要给它抛出args和kwargs。

Example: 例:

 1 #!/usr/bin/python
 2 #current's number of threads
 3 import threading
 4 import time
 5 
 6 def worker():
 7     print "test"
 8     time.sleep(1)
 9 
 10 for i in xrange(5):
 11     t = threading.Thread(target=worker)
 12     t.start()
 13 
 14 print "current has %d threads" % (threading.activeCount() - 1)

target=worker(). target = worker()。
target = worker()

target=worker. 目标=工人。
目标=工人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM