0

I am facing a small issue in my code. I have a main function that, given a certain condition arises, has to launch one or more different functions which deal with web scraping, in particular they use Selenium. The problem is that I would simply like to launch this web scraping "task", which is simply a python function, and not wait for it to terminate, rather let it go on independently from the rest of my code, so that I might be independently running 5 different instances of the same function, without waiting for them to terminate. Some pseudo code:

while True:
    condition = SomeComputation()
    if(condition):
        IndependentFunction( some_parameter )

Once IndependtFunction is called, I would like to not have to wait for it to end. I have looked at multiprocessing, but from what I understood I might not need such type of parallelisation. Thanks!

Leo
  • 45
  • 7

2 Answers2

0

If you're not depending on the output of that scraping then you could use threading

it would be like

mytask = threading.Thread(myfunction,args=(arg1,arg2,argn,))
mytask.start()

more details documentation: https://docs.python.org/3/library/threading.html

0

You would need multithreading in order to do that. The basic usage of threading module on par with your independent function could be like this:

 import threading
 while True:
     condition = SomeComputation()
     if(condition):
         newThread = threading.Thread(target=IndependentFunction, args=(some_parameter,), daemon=True)
         newThread.start()

That daemon=True argument means that the thread will execute fully independently, and the main program will not wait for it to finish what it is doing before quitting the program entirely. Check this page for a more detailed tutorial.

Nastor
  • 638
  • 4
  • 15