I am trying to make 2 functions run at the same time. Show
Does anyone know how to do this?
asked Jun 2, 2010 at 11:16
1 Do this:
answered Jun 2, 2010 at 11:19
chrisgchrisg 39k35 gold badges85 silver badges106 bronze badges 7 The answer about threading is good, but you need to be a bit more specific about what you want to do. If you have two functions that both use a lot of CPU, threading (in CPython) will probably get you nowhere. Then you might want to have a look at the multiprocessing module or possibly you might want to use jython/IronPython. If CPU-bound performance is the reason, you could even implement things in (non-threaded) C and get a much bigger speedup than doing two parallel things in python. Without more information, it isn't easy to come up with a good answer.
answered Jun 2, 2010 at 11:38
Mattias NilssonMattias Nilsson 3,5741 gold badge23 silver badges28 bronze badges This can be done elegantly with Ray, a system that allows you to easily parallelize and distribute your Python code. To parallelize your example, you'd need to define your functions with the
If
There are a number of advantages of using Ray over the multiprocessing module or using multithreading. In particular, the same code will run on a single machine as well as on a cluster of machines. For more advantages of Ray see this related post. answered Feb 4, 2019 at 0:39
Ion StoicaIon Stoica 6879 silver badges7 bronze badges 0 One option, that looks like it makes
two functions run at the same However, it has a small delay, as an Official Python Documentation Also, there's other Python modules that can be used for asynchronous execution (two pieces of code working at the same time). For some information about them and help to choose one, you can read this Stack Overflow question. Comment from another user about the threading module
– Jonas Elfström Jun 2 '10 at 11:39 Quote from the Documentation about threading module not working
answered Oct 11, 2015 at 11:07
EdwardEdward 1,0241 gold badge18 silver badges39 bronze badges 4 The thread module does work simultaneously unlike multiprocess, but the timing is a bit off. The code below prints a "1" and a "2". These are called by different functions respectively. I did notice that when printed to the console, they would have slightly different timings.
Output: (Note the space is for the wait in between printing)
Not sure if there is a way to correct this, or if it matters at all. Just something I noticed.
answered Jul 28, 2018 at 4:59
Try this
answered Jun 27, 2018 at 13:34
2 I think what you are trying to convey can be achieved through multiprocessing. However if you want to do it through threads you can do this. This might help
answered Aug 14, 2018 at 12:22
test using APscheduler:
i got these results. which proves they are running at the same time.
answered Dec 15, 2020 at 10:14
In case you also want to wait until both functions have been completed:
answered Jul 4, 2021 at 10:28
thanos.athanos.a 1,7232 gold badges26 silver badges25 bronze badges How do I start multiple threads at the same time in Python?To implement threading in Python, you have to perform three steps: Inherit the class that contains the function you want to run in a separate thread by using the Thread class. Name the function you want to execute in a thread run() . Call the start() function from the object of the class containing the run() method.
How do I run a sequentially thread in Python?Currently in python, threads get changed after executing some specified amount of bytecode instructions. They don't run at the same time. You will only have threads executing in parallel when one of them calls some I/O-intensive or not python-affecting module that can release GIL (global interpreter lock).
How do you run multiple items at once in Python?The most common way is to use the threading module. Threading allows you to create “threads” that will execute independently of each other. Another way to run multiple functions simultaneously is to use the multiprocessing module, which allows you to create “processes” that will execute independently of each other.
Why Python is not good for multithreading?Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.
|