python 如何同时运行两个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run two functions simultaneously
提问by chrissygormley
I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check the speed the camera is set to. When I check the speed the camera has stopped so no speed is available. Is there a way I can get these functions to run at the same time to test the speed of the camera. Sample code is below:
我正在运行测试,但我想同时运行 2 个功能。我有一个相机,我告诉它通过肥皂水移动,然后我通过 SSH 登录相机以检查相机设置的速度。当我检查速度时,相机已停止,因此没有速度可用。有没有办法让这些功能同时运行以测试相机的速度。示例代码如下:
class VerifyPan(TestAbsoluteMove):
def runTest(self):
self.dest.PanTilt._x=350
# Runs soap move command
threading.Thread(target = SudsMove).start()
self.command = './ptzpanposition -c 0 -u degx10'
# Logs into camera and checks speed
TestAbsoluteMove.Ssh(self)
# Position of the camera verified through Ssh (No decimal point added to the Ssh value)
self.assertEqual(self.Value, '3500')
I have now tried the threading module as mentioned below. The thread does not run in sync with the function TestAbsoluteMove.Ssh(). Is there any other code I need to make this work.
我现在已经尝试了下面提到的线程模块。该线程不会与函数 TestAbsoluteMove.Ssh() 同步运行。是否有任何其他代码需要我完成这项工作。
I have looked at putting arguments into the thread statement that state the thread runs when the Ssh() function. Does anyone know what to enter in this statement?
我已经看过将参数放入线程语句中,声明线程在 Ssh() 函数时运行。有谁知道在这个声明中输入什么?
Sorry if I haven't explained correctly. The 'SudsMove' function moves the camera and the 'Ssh' function logs into the camera and checks the speed the camera is currently moving at. The problem is that by the time the 'Ssh' function logs in the camera has stopped. I need both functions to run in parallel so I can check the camera speed while it is still moving.
对不起,如果我没有正确解释。'SudsMove' 函数移动相机,'Ssh' 函数登录到相机并检查相机当前移动的速度。问题是,当“Ssh”功能登录到相机时已经停止。我需要两个函数并行运行,以便在相机仍在移动时检查它的速度。
Thanks
谢谢
采纳答案by Eric O Lebigot
If you want to use the common Python implementation (CPython), you can certainly use the multiprocessingmodule, which does wonders(you can pass non-pickleable arguments to subprocesses, kill tasks,…), offers an interface similar to that of threads, and does not suffer from the Global Interpreter Lock.
如果你想使用通用的 Python 实现(CPython),你当然可以使用multiprocessing模块,它确实很神奇(你可以将不可pickleable 参数传递给子进程,杀死任务,...),提供类似于线程的接口,并且不受全局解释器锁定的影响。
The downside is that subprocesses are spawned, which takes more time than creating threads; this should only be a problem if you have many, many short tasks. Also, since data is passed (via serialization) between processes, large data both takes a long time to pass around and ends up having a large memory footprint (as it is duplicated between each process). In situations where each task takes a "long" time and the data in and out of each task is not too large, the multiprocessingmodule should be great.
缺点是会产生子进程,比创建线程需要更多时间;如果您有很多很多短期任务,这应该只是一个问题。此外,由于数据在进程之间传递(通过序列化),大数据既需要很长时间才能传递,并且最终会占用大量内存(因为它在每个进程之间重复)。在每个任务需要“很长时间”并且每个任务进出的数据不是太大的情况下,多处理模块应该很棒。
回答by AndiDog
Import the threading
module and run SudsMove()
like so:
导入threading
模块并SudsMove()
像这样运行:
threading.Thread(target = SudsMove).start()
That will create and start a background thread which does the movement.
这将创建并启动一个执行运动的后台线程。
ANSWER TO EDITED QUESTION:
对编辑问题的回答:
As far as I understand this, TestAbsoluteMove.Ssh(self)
polls the speed once and stores the result in self.Value
?! And you're testing the expected end tilt/rotation/position with self.assertEqual(self.Value, '3500')
?!
据我了解,TestAbsoluteMove.Ssh(self)
轮询一次速度并将结果存储在self.Value
?! 并且您正在使用self.assertEqual(self.Value, '3500')
?!测试预期的末端倾斜/旋转/位置。
If that's correct, you should wait for the camera to start its movement. You could probably poll the speed in a certain interval:
如果这是正确的,您应该等待相机开始移动。您可能可以在某个时间间隔内轮询速度:
# Move camera in background thread
threading.Thread(target = SudsMove).start()
# What does this do?
self.command = './ptzpanposition -c 0 -u degx10'
# Poll the current speed in an interval of 250 ms
import time
measuredSpeedsList = []
for i in xrange(20):
# Assuming that this call will put the result in self.Value
TestAbsoluteMove.Ssh(self)
measuredSpeedsList.append(self.Value)
time.sleep(0.25)
print "Measured movement speeds: ", measuredSpeedsList
The movement speed will be the biggest value in measuredSpeedsList
(i.e. max(measuredSpeedsList)
). Hope that makes sense...
移动速度将是measuredSpeedsList
(即max(measuredSpeedsList)
)中的最大值。希望这是有道理的...
回答by kgiannakakis
回答by Joonas Pulakka
If you can get your code to run under Jython or IronPython, then you canrun several threads simultaneously; they don't have that goofy "Global Interpreter Lock" thing of CPython.
如果您可以让您的代码在 Jython 或 IronPython 下运行,那么您可以同时运行多个线程;他们没有 CPython 那种愚蠢的“全局解释器锁”。