0

I am a beginner in python and multiprocessing so if the question seems naive please forgive me. I have two functions that I want to run at the same time. One is an openCV implementation of face recognition and the other is a standard python code.

def main():
    does(s) # a function call 

def face():
    recog.main() #another call

As you can guess, both the functions are the final end user functions that one has to call to implement the task. I want them both to run simutaneously.

Previous answers on this topic advise threading module but I have tried it and it does not work. The first func. to be called is executed first and then the second one. A friend of mine recommended rospy module. Is it the only way? Thanks in anticipation.

EDIT: In the answer to this, Make 2 functions run at the same time , a user has written that threading actually won't make two functions run at the same time

3
  • 1
    Previous answers on this topic advise threading module but I have tried it and it does not work What did you try, Provide a minimal reproducible example. Also take a look at Dead simple example of using Multiprocessing Queue, Pool and Locking Commented Jun 15, 2016 at 8:07
  • thread.start_new_thread(main()) thread.start_new_thread(face()) This does not run both of them simultaneously. Instead, finishes them up one by one. I tried threading module tutorialspoint.com/python/python_multithreading.htm But the result was the same. Commented Jun 15, 2016 at 8:25
  • If you aren't using ROS anyway, don't use rospy only running things in parallel. This is not what it is meant for and it will give you a unnecessary dependency on ROS. Commented Jun 16, 2016 at 12:25

1 Answer 1

1

I use the multiprocessing module for running two functions parallel. For what I did (changed to your situation):

import multiprocessing

def main():
    does(s) # a function call 

def face():
    recog.main() #another call

# Initiate two workers for the two functions
workerMAIN = multiprocessing.Process(target=main)
workerFACE = multiprocessing.Process(target=face)

# Start the workers
workerMAIN.start()
workerFACE.start()

# Wait until the functions have finished
workerMAIN.join()
workerFACE.join()
Sign up to request clarification or add additional context in comments.

3 Comments

A big thanks SIr! I had tried the multiprocessing module out using the target keyword but it had not worked. A follow-up question, though. Is name a necessary argument? Thanks
No it isn't: in the example in 16.6.1.1 you can see Process(target=f, args=('bob',)). So you can remove the name part! I will update my answer :)
For me, this was also the example I was looking for. Like @Spock I looked at proposed duplicates but wasn't able to get it working untill I implemented the code above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.