Python multiprocessing ⚡

preview_player
Показать описание
python multiprocessing tutorial example explained

#python multiprocessing #tutorial #example #explained

# ***********************************
# Python multiprocessing
# ***********************************
# multiprocessing = running tasks in parallel on different cpu cores, bypasses GIL used for threading
# multiprocessing = better for cpu bound tasks (heavy cpu usage)
# multithreading = better for io bound tasks (waiting around)
# ***********************************

Bro Code merch store 👟 :
===========================================================
===========================================================

music credits 🎼 :
===========================================================
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
===========================================================
Рекомендации по теме
Комментарии
Автор

#
# Python multiprocessing
#
# multiprocessing = running tasks in parallel on different cpu cores, bypasses GIL used for threading
# multiprocessing = better for cpu bound tasks (heavy cpu usage)
# multithreading = better for io bound tasks (waiting around)

from multiprocessing import Process, cpu_count
import time


def counter(num):
count = 0
while count < num:
count += 1


def main():

print("cpu count:", cpu_count())

a = Process(target=counter,
b = Process(target=counter,

a.start()
b.start()

print("processing...")

a.join()
b.join()

print("Done!")
print("finished in:", time.perf_counter(), "seconds")


if __name__ == '__main__':
main()

#

BroCodez
Автор

Hi Bro! I have been following this course as I teach myself coding and it has been a HUGE help! I did have question about the "time.perf_counter()" call. For whatever reason whenever I run this it returns with something like this : "finished in: 584461.4056658 seconds" when in reality it took about 11 seconds to finish. I am not sure why this is occurring as I have typed out everything that same you have. Thanks again for all the videos, they have been fantastic!

calebknepley
Автор

In case timer is bad for you as well add it to counter function. Add at the start:
start_time = time.perf_counter()

Then at the end
end_time = time.perf_counter()
print("Time taken by counter: ", end_time - start_time, "seconds")

benderbg
Автор

Finding this extremely helpful even 2 years later! you are the best!

luiserasmoperaltafernandez
Автор

I came as beginner, and now feel quite assured as an intermediate. Thanks so much

wu
Автор

your tutorials are excellent....thanks Bro...!

muralikrishna
Автор

Hey you, Yes! I am talking to you, it's a nice video

rgdmwob
Автор

Excelent video bro, leaving my comment.

kennerges
Автор

Thank you very much. It was a great explanation.

iaroslavz
Автор

finally!!! i have got some clear understandings.

erick
Автор

Hi, is correct? Multiprocessing is SAME CALCATION parralel. Multithread is Run any tasks in same time on all threads prcoessor

TailsFinance
Автор

I understand that Python multi-processing and multi-threading can accelerate running independent tasks. However, If I have 10 tasks sequentially and each one's input rely on the previous one's output, is it still possible to accelerate using parallel computing or parallel processing? Any perspective from software or hardware will be appreciated!

chuanjiang
Автор

Thanks! great content and explanation! Can you help with instruction how to combine concurrency (such as asyncio or asyncioscheduler with multiprocessing)? Example, I want 2 processes dedicated to computing, and the other 2 dedicated for I/O tasks.

kylevan
Автор

In this program, to count the numbers, can we also use multi threading? Why is multi processing is over multi threading when counting?

RenovaLabs
Автор

@everyone I have a question. How does it come, that my time.perf_counter() does not give out the time, since the programm started. If I want to achieve that, I have to type in x = time.perf_counter() at the start of the programm and subtract the perf_counter() from the end by x and then I get the right time. Sorry if my english is not very good. Hope someone can help me with that problem

nicow
Автор

doesnt work for me at all. ImportError: cannot import name 'Process' from partially initialized module 'multiprocessing' (most likely due to a circular import) it looks like those functions doesn't exist in library or sth ? also already in this part "from multiprocessing import Process, cpu_count" it cant find reference to Process, cpu_count...any ideas what's going on?

michaelk
Автор

Hi bro i need some help here on my project on java

bourenaneayoubabdelmadid
Автор

Hi Bro

def main():
print("cpu count:", cpu_count())

If I use:
a =
b =

I get:
cpu count: 8
processing...
Done!
finished in: 43 seconds

instead of:
a = Process(target=counter,
b = Process(target=counter,

I get the same results but
cpu count: 8
processing...
Done!
finished in: 22 seconds

It seem I get the same results but the time comsuption is so much higher .... Any explanation.... Thanks

JoseHernandez-qkby
Автор

Hey Bro, my code is giving me several errors... I'm not sure what to do, could you help me out?

hahaupscalinh