下面显示的代码是我用来控制7种不同的9g伺服电机的代码。它们连接到adafruit伺服帽,连接到树莓派3。所有7个定义都可以并行或同时运行。使用我拥有的代码,这7个定义确实可以并行运行,但是并不一致。我的意思是,该代码有时会完全起作用,而其他时候,伺服器会在操作过程中停止。似乎我需要使用多处理功能而不是Thread功能来获得更多的计算能力。回到主要问题,是否可以在python中使用具有多个定义的并行处理函数,该怎么做?谢谢
from __future__ import division
import sys
import time
import atexit
import threading
import subprocess
import multiprocessing
from threading import Thread
# Import the PCA9685 module.
import Adafruit_PCA9685
import ServoMotorCommandCenter
pwm = Adafruit_PCA9685.PCA9685()
a = ServoMotorCommandCenter.ServoMotorCommandCenter()
def func1():
print '1'
time.sleep(2)
pwm.set_pwm(1, 0, 375)
a.SMF(1, 275, 375, 0.01)
time.sleep(6)
a.SMB(1, 275, 375, 0.01)
time.sleep(4)
pwm.set_pwm(1, 0, 0)
time.sleep(1)
print '1'
def func2():
print '2'
time.sleep(2)
pwm.set_pwm(5, 0, 450)
a.SMB(5, 150, 450, 0.001)
time.sleep(7)
a.SMF(5, 150, 450, 0.0001)
time.sleep(4)
pwm.set_pwm(5, 0, 0)
time.sleep(1)
print '2'
def func3():
print '3'
time.sleep(2)
a.SMF(2, 150, 300, 0.001)
time.sleep(5)
a.SMB(2, 150, 300, 0.001)
time.sleep(1)
pwm.set_pwm(2, 0, 0)
time.sleep(1)
print '3'
def func4():
print '4'
time.sleep(3)
pwm.set_pwm(3, 0, 0)
a.SMBA(3, 150, 450, 2, 0.001)
time.sleep(2)
a.SMFA(3, 150, 450, 2, 0.01)
time.sleep(1)
pwm.set_pwm(3, 0, 0)
time.sleep(1)
print '4'
def func5():
print '5'
time.sleep(3)
pwm.set_pwm(4, 0, 0)
a.SMFA(4, 150, 450, 2, 0.001)
time.sleep(2)
a.SMBA(4,150, 450, 2, 0.01)
time.sleep(1)
pwm.set_pwm(4, 0, 0)
time.sleep(1)
print '5'
def func6():
print '6'
time.sleep(3)
pwm.set_pwm(7, 0, 0)
a.SMBA(7, 150, 450, 2, 0.001)
time.sleep(2)
a.SMFA(7, 150, 450, 2, 0.01)
time.sleep(1)
pwm.set_pwm(7, 0, 0)
time.sleep(1)
print '6'
def func7():
print '7'
time.sleep(3)
pwm.set_pwm(6, 0, 0)
a.SMFA(6, 150, 450, 2, 0.001)
time.sleep(2)
a.SMBA(6,150, 450, 2, 0.01)
time.sleep(1)
pwm.set_pwm(6, 0, 0)
time.sleep(1)
print '7'
if __name__ == '__main__':
Thread(target = func1).start()
Thread(target = func2).start()
Thread(target = func3).start()
Thread(target = func4).start()
Thread(target = func5).start()
Thread(target = func6).start()
Thread(target = func7).start()