Python 加入后线程未关闭
Python threads are not closing after join
我正在使用与下面类似的代码来控制 rgb led 灯带,随机值每隔几秒出现一次,并且正在创建 3 个线程来控制 3 种单独的颜色。
import time
from ast import literal_eval
import threading
from random import randint
t=[]
myR =0
myG =0
myB =0
temp = 0
pins = [17,22,24]
myColours = [myR,myG,myB]
red_pin = 17
green_pin = 22
blue_pin = 24
def change_led_color(rgb):
global myR
global myG
global myB
y = 0
threads = []
for colour in rgb:
t = threading.Thread(target=leds,name=pins[y] ,args=(y,myColours[y],colour,pins[y]))
threads.append(t)
y += 1
for y in threads:
y.start()
for y in threads:
y.join()
def leds(index,start,end,pin):
temp=0
for i in range(start,end):
time.sleep(0.01)
temp = i
global myColours
print 'pin', pin, 'started at: ',start,' ended is: ', temp
myColours[index] = end
def set_colours():
print '..................................................................',t
print threading.activeCount(),'threads \n'
threading.Timer(2, set_colours).start()
change_led_color(t)
set_colours()
def get_data():
while True:
global t
t = (randint(0,255),randint(0,255),randint(0,255))
time.sleep(2)
threading.Thread(target=get_data).start()
上面的运行很好,但是反应很奇怪,我不是在线程的末尾得到所有三种颜色,有时我得到的比预期的多,而且大多数时候至少有一种会为 0,就像线程从不 运行!
我假设我以某种方式滥用线程..
例如结果
.................................................................. (187, 223, 42)
3 threads
pin 24 started at: 205 ended is: 0
pin 22 started at: 170 ended is: 222
pin 17 started at: 107 ended is: 186
.................................................................. (202, 115, 219)
3 threads
pin 22 started at: 223 ended is: 0
pin 17 started at: 187 ended is: 201
.................................................................. (244, 35, 194)
5 threads
pin 22 started at: 115 ended is: 0
pin 24 started at: 42 ended is: 218
pin 17 started at: 202 ended is: 243
pin 24 started at: 42 ended is: 193
.................................................................. (54, 25, 72)
3 threads
pin 17 started at: 244 ended is: 0
pin 22 started at: 35 ended is: 0
pin 24 started at: 194 ended is: 0
你考虑过GIL(全局解释器锁)吗?
https://wiki.python.org/moin/GlobalInterpreterLock
Python 有一个 GIL,它不允许来自单个进程的多个线程 运行。当您 运行 您的代码时,它是 运行 作为一个 python 过程。您正在尝试从 python 中不允许的同一进程启动线程。
如果你想并行执行一些操作,多处理包是一个不错的选择。
https://pymotw.com/2/multiprocessing/basics.html
在不考虑其他因素(见下文)的情况下,for i in range(start,end):
循环将持续长达 2.5 秒;但实际上你每 2 秒就会产生一个新线程。
此外,该循环的实际持续时间实际上更长:
- 每当你要求睡觉时,持续时间至少是你要求的;但它可能会稍微多一点。尤其是你要求1/100秒的时候,可能会长一点。
- 既然你要求一个接一个地睡很多小觉,你可能希望效果成倍增加;这意味着在循环中花费的总时间
for i in range(start,end):
将比预期的要长
尝试:
- 使用一次等待:
time.sleep(0.01 * start-end if end>start else 0)
- 将用于生成新线程的时间增加到 3-4 秒
我正在使用与下面类似的代码来控制 rgb led 灯带,随机值每隔几秒出现一次,并且正在创建 3 个线程来控制 3 种单独的颜色。
import time
from ast import literal_eval
import threading
from random import randint
t=[]
myR =0
myG =0
myB =0
temp = 0
pins = [17,22,24]
myColours = [myR,myG,myB]
red_pin = 17
green_pin = 22
blue_pin = 24
def change_led_color(rgb):
global myR
global myG
global myB
y = 0
threads = []
for colour in rgb:
t = threading.Thread(target=leds,name=pins[y] ,args=(y,myColours[y],colour,pins[y]))
threads.append(t)
y += 1
for y in threads:
y.start()
for y in threads:
y.join()
def leds(index,start,end,pin):
temp=0
for i in range(start,end):
time.sleep(0.01)
temp = i
global myColours
print 'pin', pin, 'started at: ',start,' ended is: ', temp
myColours[index] = end
def set_colours():
print '..................................................................',t
print threading.activeCount(),'threads \n'
threading.Timer(2, set_colours).start()
change_led_color(t)
set_colours()
def get_data():
while True:
global t
t = (randint(0,255),randint(0,255),randint(0,255))
time.sleep(2)
threading.Thread(target=get_data).start()
上面的运行很好,但是反应很奇怪,我不是在线程的末尾得到所有三种颜色,有时我得到的比预期的多,而且大多数时候至少有一种会为 0,就像线程从不 运行! 我假设我以某种方式滥用线程..
例如结果
.................................................................. (187, 223, 42)
3 threads
pin 24 started at: 205 ended is: 0
pin 22 started at: 170 ended is: 222
pin 17 started at: 107 ended is: 186
.................................................................. (202, 115, 219)
3 threads
pin 22 started at: 223 ended is: 0
pin 17 started at: 187 ended is: 201
.................................................................. (244, 35, 194)
5 threads
pin 22 started at: 115 ended is: 0
pin 24 started at: 42 ended is: 218
pin 17 started at: 202 ended is: 243
pin 24 started at: 42 ended is: 193
.................................................................. (54, 25, 72)
3 threads
pin 17 started at: 244 ended is: 0
pin 22 started at: 35 ended is: 0
pin 24 started at: 194 ended is: 0
你考虑过GIL(全局解释器锁)吗? https://wiki.python.org/moin/GlobalInterpreterLock
Python 有一个 GIL,它不允许来自单个进程的多个线程 运行。当您 运行 您的代码时,它是 运行 作为一个 python 过程。您正在尝试从 python 中不允许的同一进程启动线程。
如果你想并行执行一些操作,多处理包是一个不错的选择。
https://pymotw.com/2/multiprocessing/basics.html
在不考虑其他因素(见下文)的情况下,for i in range(start,end):
循环将持续长达 2.5 秒;但实际上你每 2 秒就会产生一个新线程。
此外,该循环的实际持续时间实际上更长:
- 每当你要求睡觉时,持续时间至少是你要求的;但它可能会稍微多一点。尤其是你要求1/100秒的时候,可能会长一点。
- 既然你要求一个接一个地睡很多小觉,你可能希望效果成倍增加;这意味着在循环中花费的总时间
for i in range(start,end):
将比预期的要长
尝试:
- 使用一次等待:
time.sleep(0.01 * start-end if end>start else 0)
- 将用于生成新线程的时间增加到 3-4 秒