如何同时运行两个功能 [英] How to run two functions simultaneously

查看:123
本文介绍了如何同时运行两个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行测试,但我想同时运行2个函数。我有一个摄像头,我告诉它通过泡沫移动,然后通过SSH登录到摄像头以检查摄像头设置的速度。当我检查速度时,相机已停止,因此没有速度可用。有没有办法让我可以同时运行这些功能来测试相机的速度。示例代码如下:

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:

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

现在我已经尝试了下面提到的线程模块。该线程不会与函数TestAbsoluteMove.Ssh()同步运行。是否还有其他代码需要我做这件事。

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.

我已经研究了将参数放入线程语句中,以便在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?

对不起,如果我没有正确解释。 'SudsMove'功能可将相机和'Ssh'功能记录到相机中,并检查相机当前正在移动的速度。问题在于,'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.

谢谢

Thanks

解决方案

rel =nofollow noreferrer> multiprocessing module,which

If you want to use the common Python implementation (CPython), you can certainly use the multiprocessing module, 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.

不足之处在于子流程会产生,这比创建线程需要更多的时间;如果你有很多很短的任务,这应该只是一个问题。在每个任务需要很长时间的情况下,多处理模块应该很棒。

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. In situations where each task takes a "long" time, the multiprocessing module should be great.

这篇关于如何同时运行两个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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