简体   繁体   English

如何同时运行两个函数

[英]How to run two functions simultaneously

I am running test but I want to run 2 functions at the same time.我正在运行测试,但我想同时运行 2 个功能。 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.我有一个相机,我告诉它通过泡沫移动,然后我通过 SSH 登录相机以检查相机设置的速度。 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')

I have now tried the threading module as mentioned below.我现在已经尝试了如下所述的线程模块。 The thread does not run in sync with the function TestAbsoluteMove.Ssh().该线程不会与函数 TestAbsoluteMove.Ssh() 同步运行。 Is there any other code I need to make this work.是否有任何其他代码需要我完成这项工作。

I have looked at putting arguments into the thread statement that state the thread runs when the Ssh() function.我已经看过将参数放入线程语句中,该语句声明线程在 Ssh() 函数时运行。 Does anyone know what to enter in this statement?有谁知道在这个声明中输入什么?

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. 'SudsMove' 函数移动相机,'Ssh' 函数登录到相机并检查相机当前移动的速度。 The problem is that by the time the 'Ssh' function logs in the camera has stopped.问题在于,当“Ssh”功能登录到相机时已经停止。 I need both functions to run in parallel so I can check the camera speed while it is still moving.我需要两个函数并行运行,以便我可以在相机仍在移动时检查它的速度。

Thanks谢谢

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 ?!据我了解, TestAbsoluteMove.Ssh(self)轮询一次速度并将结果存储在self.Value ?! And you're testing the expected end tilt/rotation/position with self.assertEqual(self.Value, '3500') ?!并且您正在使用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 (ie max(measuredSpeedsList) ).移动速度将是measuredSpeedsList速度列表中的max(measuredSpeedsList)max(measuredSpeedsList) )。 Hope that makes sense...希望这是有道理的...

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.如果你想使用通用的 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 multiprocessing module should be great.在每个任务需要“很长时间”并且每个任务进出的数据不是太大的情况下,多处理模块应该很棒。

There can only be one thread running at the same time.只能有一个线程同时运行。 This has been answered extensively here .这已经在这里得到了广泛的回答。 One solution will be to use two separate processes.一种解决方案是使用两个单独的进程。 The above answer provides some tips.上面的回答提供了一些提示。

If you can get your code to run under Jython or IronPython, then you can run several threads simultaneously;如果您可以让您的代码在 Jython 或 IronPython 下运行,那么您可以同时运行多个线程; they don't have that goofy "Global Interpreter Lock" thing of CPython.他们没有 CPython 那种愚蠢的“全局解释器锁”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM