多线程不起作用 [英] multithreading not working

查看:162
本文介绍了多线程不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@KSFT

当前由于无法解析stackoverflow上的格式或使简单的python脚本正常工作而感到沮丧...

currently frustrated by my inability to either decipher the formatting on stackoverflow OR make a simple python script work...

这是怎么了?

程序提示输入以确定停顿值,但不会导致led亮起.

the program prompts for input to determine the value of dwell but it does not result in an led turning on.

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())   

错了!

input_thread=threading.Thread(target=get_input) 

是对的!

线程

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

如果要将arg赋予get_input,则需要给它抛出args和kwargs.

If you want to give arg to get_input ,you need to give it throw args and kwargs.

示例:

 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.

这篇关于多线程不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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