Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

python - How to run two functions simultaneously

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

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.

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Import the threading module and run SudsMove() like so:

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

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...