Python multiprocessing ⚡

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 79

  • @BroCodez
    @BroCodez  3 года назад +27

    # ***********************************
    # 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, args=(500000000,))
    b = Process(target=counter, args=(500000000,))
    a.start()
    b.start()
    print("processing...")
    a.join()
    b.join()
    print("Done!")
    print("finished in:", time.perf_counter(), "seconds")
    if __name__ == '__main__':
    main()
    # ***********************************

  • @benderbg
    @benderbg 6 месяцев назад +2

    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")

  • @lperalta12
    @lperalta12 Год назад

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

  • @Kartik-el3pj
    @Kartik-el3pj 2 месяца назад

    What a beautiful way of explaining stuff!🙌🙌learnt a lot! Thankss

  • @calebknepley8256
    @calebknepley8256 2 года назад +35

    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!

    • @justwei5668
      @justwei5668 2 года назад +1

      same here

    • @GrantyPanties09
      @GrantyPanties09 2 года назад +1

      THIS!^^^^^ please explain...

    • @henriquemanuelcordeirorodr2523
      @henriquemanuelcordeirorodr2523 2 года назад +10

      Hello. I've been watching the full python course and I had this same problem. I don't know if you already got a solution or even if you just stopped thinking about it. I created a variable in the beginning of the function main and one in the end (of that same function), both associated to a time.perf_counter() method. In the end i printed the difference between the last perf_counter() and the first one and now it looks like it works. I'm not sure if this is the right approach but I tried with 8 cores and, in my case, the result is between 7 and 9 seconds.

    • @caio7892
      @caio7892 2 года назад

      uppp

    • @ollkorrect2900
      @ollkorrect2900 2 года назад

      @@henriquemanuelcordeirorodr2523 Bro thank you so much this was an absolute pain for a while. It seems not known or something on the rest of the internet lol.

  • @wu1484741111
    @wu1484741111 7 месяцев назад

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

  • @cristiucvladimir7909
    @cristiucvladimir7909 5 месяцев назад

    thanks for powerful content

  • @HojiakbarHabibullayev-jj6db
    @HojiakbarHabibullayev-jj6db 23 дня назад

  • @TailsFinance777
    @TailsFinance777 Год назад +1

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

  • @AdityaRaj-e3d
    @AdityaRaj-e3d 9 месяцев назад

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

  • @muralikrishna1785
    @muralikrishna1785 Год назад

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

  • @imadiman528
    @imadiman528 8 месяцев назад

    You're awesome

  • @rubenc4696
    @rubenc4696 5 месяцев назад

    thanks

  • @cristiucvladimir7909
    @cristiucvladimir7909 5 месяцев назад

    thanks bro

  • @pandaplayz1283
    @pandaplayz1283 5 месяцев назад

    amazing

  • @kennerges2841
    @kennerges2841 3 года назад

    Excelent video bro, leaving my comment.

  • @erick5171
    @erick5171 2 года назад

    finally!!! i have got some clear understandings.

  • @Monsta1291
    @Monsta1291 Год назад +1

    legend!!!!!😍

  • @infinitecrafterYT
    @infinitecrafterYT Год назад

    Nice video BROOOO

  • @iaroslavz2644
    @iaroslavz2644 2 года назад

    Thank you very much. It was a great explanation.

  • @nibrasmuhammed5105
    @nibrasmuhammed5105 3 года назад +1

    bro is soooper cool!!

  • @chuanjiang6931
    @chuanjiang6931 5 месяцев назад

    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!

    • @catsaregoofy
      @catsaregoofy Месяц назад

      It's possible, but it will not provide a significant performance boost

  • @GTV_367
    @GTV_367 Год назад +1

    Was this done on Intel or amd cpu

  • @RenovaLabs
    @RenovaLabs 6 месяцев назад

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

  • @LargeBeefyMan
    @LargeBeefyMan Год назад

    I love you brocode

  • @piotrkopcewicz5227
    @piotrkopcewicz5227 Год назад

    great !!

  • @SuperStarEevee
    @SuperStarEevee 2 года назад

    Thank you!

  • @HussainAli-sb1dv
    @HussainAli-sb1dv 11 месяцев назад

    LOve u

  • @kylevan2060
    @kylevan2060 2 года назад

    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.

  • @beingzero7541
    @beingzero7541 2 года назад

    Wow!

  • @pascal_4732
    @pascal_4732 3 года назад

    Thank you😄

  • @darkwolf94728
    @darkwolf94728 Год назад

    thanks bro! really helped

  • @bekturasanbekov1979
    @bekturasanbekov1979 Год назад

    thx 4 vid bro !

  • @uuhju7004
    @uuhju7004 3 года назад

    nice video Bro!

  • @lw9954
    @lw9954 2 года назад

    Ty bro

  • @michael3k80
    @michael3k80 2 года назад +1

    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?

    • @michael3k80
      @michael3k80 2 года назад +2

      solved : i named the whole file the same way (multiprocessing) after changing interpreter suddenly found what was needed.

  • @nicow8634
    @nicow8634 Год назад +1

    @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

  • @JoseHernandez-qk5by
    @JoseHernandez-qk5by 10 месяцев назад

    Hi Bro
    def main():
    print("cpu count:", cpu_count())
    If I use:
    a = Process(target=counter(500000000))
    b = Process(target=counter(500000000))
    I get:
    cpu count: 8
    processing...
    Done!
    finished in: 43 seconds
    instead of:
    a = Process(target=counter, args=(500000000,))
    b = Process(target=counter, args=(500000000,))
    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

  • @arkamukhopadhyay2177
    @arkamukhopadhyay2177 3 года назад +2

    Hey bro, when the complete task was divided into two processes, why was the time taken not halved?
    and, are there any disadvantages of processes that threads doesn't have? You advised to use threads for io bound operations in a previous video...
    Thanks!

    • @kamoteq6672
      @kamoteq6672 3 года назад +2

      I don't exactly know the full reason, but like Bro Code mentioned: every time you create a process there is a significant overhead in terms of time. Which is part of the reason why it's not exactly half.

    • @scooby-dooafterlife6969
      @scooby-dooafterlife6969 3 года назад

      maybe processor boosting the 1 core higher, more than it could with 2 cores or just python not being very efficient

  • @hahaupscalinh7872
    @hahaupscalinh7872 7 месяцев назад

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

  • @philtoa334
    @philtoa334 3 года назад

    : )

  • @gustavoaponte1814
    @gustavoaponte1814 Год назад

    meow~! uwu

  • @ScimitarGaming
    @ScimitarGaming 2 года назад

    hey bro, in my pc perf_counter is returning an unusually large number 20000 seconds, why?? and how to fix it??

    • @printinghouseimp6739
      @printinghouseimp6739 Год назад +1

      I think it is using another time as its reference. I had similar issue. I was able to fix by defining t1 = time.perf_counter() at beginning, t2 = time.perf_counter() at end, then printing t2 - t1 seconds.

  • @bourenaneayoubabdelmadid5211
    @bourenaneayoubabdelmadid5211 3 года назад

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

  • @madhubabu-hq4yc
    @madhubabu-hq4yc 11 месяцев назад

    I have 16 core processor so I can create 16 processes at once ☺

  • @ryanstephen120
    @ryanstephen120 Год назад

    ta

  • @jhassee
    @jhassee 2 года назад

    im on a beast of a pc so 8 of em was 8 seconds, 150 000 000 per second

  • @RoadToDP
    @RoadToDP 10 месяцев назад

    a

  • @Jens1111
    @Jens1111 5 месяцев назад

    E

  • @duke9815
    @duke9815 2 года назад +2

    finished in: 21190.5506864 seconds
    what's wrong my cpu

    • @lucamassimosantilli8533
      @lucamassimosantilli8533 2 года назад

      Don't know man, mine says finished in 71858.8663355 seconds, even though it took like 8 seconds. Why does it say that?

    • @duke9815
      @duke9815 2 года назад

      @@lucamassimosantilli8533 Yeah same. I look at codes again and again, maybe missed some code or typed wrong code that changed the type of "sec". But nothing. Maybe someone sees and helps some other time. Cheers.

    • @raedshaher5186
      @raedshaher5186 2 года назад +16

      add this to the start : start = time.perf_counter()
      and add this to the print function : time.perf_counter()-start
      basically starting a timer and then subtracting it

    • @piggyplayer07
      @piggyplayer07 2 года назад

      @@raedshaher5186 Thank you!

    • @KapitanZbik2138
      @KapitanZbik2138 2 года назад

      @@raedshaher5186 Thanks man I've had the same issue ;)

  • @aliforootani9348
    @aliforootani9348 2 года назад

    thanks

  • @رادیندولتی
    @رادیندولتی 2 года назад

    thanks bro