Call a function without waiting for it

Hi, I was wondering if there is a way to call a function / method (preferably in Python or Java) and continue execution without waiting for it.

Example:

def a(): b() #call a function, b() return "something" def b(): #something that takes a really long time 
+10
source share
7 answers

Launch it in a new topic. Learn about Java multithreading here and Python multithreading here.

Java example:

WRONG way ... by subclassing Thread

 new Thread() { public void run() { YourFunction();//Call your function } }.start(); 

CORRECT way ... by providing a Runnable instance

 Runnable myrunnable = new Runnable() { public void run() { YourFunction();//Call your function } } new Thread(myrunnable).start();//Call it when you need to run the function 
+22
source

Using multiprocessing in python:

 from multiprocessing import Process def b(): # long process p = Process(target=b) p.start() 
+4
source

As noted in other answers, from Python you can either put the function in a new thread (not so good, since there are not so many of you in CPython) or in another process using Multiprocessing -

 from multiprocessing import Process def b(): # long process def a(): p = Process(target=b) p.start() ... a() 

(As stated in monkut's answer).

But the Python decorator allows you to hide the template under the carpet, so that during the call you only see a regular function call. In the example below, I am creating a "parallel" decorator - just place it in front of any function and it will automatically start in a separate process when called:

 from multiprocessing import Process from functools import partial from time import sleep def parallel(func): def parallel_func(*args, **kw): p = Process(target=func, args=args, kwargs=kw) p.start() return parallel_func @parallel def timed_print(x=0): for y in range(x, x + 10): print y sleep(0.2) def example(): timed_print(100) sleep(0.1) timed_print(200) for z in range(10): print z sleep(0.2) if __name__ == "__main__": example() 

When you run this fragment, you get:

 [ gwidion@caylus Documents]$ python parallel.py 100 0 200 101 1 201 102 2 202 103 3 203 104 4 204 105 5 205 106 6 206 107 7 207 108 8 208 109 9 209 [ gwidion@caylus Documents]$ 
+4
source

Java has a standard idiom: create a thread and run it:

 new Thread() { @Override public void run() { callMyFunction(); } }.start(); 

Or you can create a Runnable and pass it to the stream:

 Runnable caller = new Runnable() { @Override public void run() { callMyFunction(); } } new Thread(caller).start(); 
+3
source

It is better to start with ExecutorService instead of going directly with raw threads. It provides merging, completion of discovery, and there are subclasses that also have some planning. For instance:

 ... // Create a simple instance with a single thread in the pool ExecutorService executor = Executors.newFixedThreadPool(1); ... Future<Integer> future = executor.submit(new Callable<Integer>() { @Override public Integer call() { return YourFunction(); } }); ... // To wait for YourFunction() to finish, and get the result: Integer result = future.get(); 

You can send as many asynchronous tasks to the ExecutorService as much as you want; they will be executed in parallel or sequentially, depending on the implementation you choose, on the number of threads in the pool of supporting threads, etc.

+1
source

just call this function in a new thread ...

0
source

Since jsbueno answer will not work on all Windows systems, since os.fork will not work there efficiently due to OS limitations, you will have to use multithreading there to achieve the same effect.

Please find the same code from jsbueno's answer with multi-threading instead of multi-processor (Python version 3)

 from threading import Thread from time import sleep def thread_parallel(func): def parallel_func(*args, **kw): p = Thread(target=func, args=args, kwargs=kw) p.daemon = True p.start() return parallel_func @thread_parallel def timed_print(x=0): for y in range(x, x + 10): print(y) sleep(0.2) def example(): timed_print(100) sleep(0.1) timed_print(200) for z in range(10): print(z) sleep(0.2) if __name__ == "__main__": example() 
0
source

Source: https://habr.com/ru/post/920384/


All Articles