Python multiprocessing ⚡

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 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()
    # ***********************************

    • @Ðogecoin
      @Ðogecoin ปีที่แล้ว +1

      Thanks bro

  • @wu1484741111
    @wu1484741111 8 หลายเดือนก่อน

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

  • @lperalta12
    @lperalta12 ปีที่แล้ว

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

  • @benderbg
    @benderbg 7 หลายเดือนก่อน +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")

  • @Kartik-el3pj
    @Kartik-el3pj 3 หลายเดือนก่อน

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

  • @cristiucvladimir7909
    @cristiucvladimir7909 6 หลายเดือนก่อน

    thanks for powerful content

  • @calebknepley8256
    @calebknepley8256 3 ปีที่แล้ว +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 3 ปีที่แล้ว +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.

  • @AdityaRaj-e3d
    @AdityaRaj-e3d 9 หลายเดือนก่อน

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

  • @HojiakbarHabibullayev-jj6db
    @HojiakbarHabibullayev-jj6db หลายเดือนก่อน

  • @pandaplayz1283
    @pandaplayz1283 6 หลายเดือนก่อน

    amazing

  • @imadiman528
    @imadiman528 9 หลายเดือนก่อน

    You're awesome

  • @muralikrishna1785
    @muralikrishna1785 ปีที่แล้ว

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

  • @iaroslavz2644
    @iaroslavz2644 2 ปีที่แล้ว

    Thank you very much. It was a great explanation.

  • @nibrasmuhammed5105
    @nibrasmuhammed5105 3 ปีที่แล้ว +1

    bro is soooper cool!!

  • @Monsta1291
    @Monsta1291 ปีที่แล้ว +1

    legend!!!!!😍

  • @infinitecrafterYT
    @infinitecrafterYT ปีที่แล้ว

    Nice video BROOOO

  • @piotrkopcewicz5227
    @piotrkopcewicz5227 ปีที่แล้ว

    great !!

  • @erick5171
    @erick5171 2 ปีที่แล้ว

    finally!!! i have got some clear understandings.

  • @kennerges2841
    @kennerges2841 3 ปีที่แล้ว

    Excelent video bro, leaving my comment.

  • @LargeBeefyMan
    @LargeBeefyMan 2 ปีที่แล้ว

    I love you brocode

  • @HussainAli-sb1dv
    @HussainAli-sb1dv ปีที่แล้ว

    LOve u

  • @bekturasanbekov1979
    @bekturasanbekov1979 ปีที่แล้ว

    thx 4 vid bro !

  • @رادیندولتی
    @رادیندولتی 3 ปีที่แล้ว

    thanks bro

  • @TailsFinance777
    @TailsFinance777 ปีที่แล้ว +1

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

  • @aliforootani9348
    @aliforootani9348 3 ปีที่แล้ว

    thanks

  • @lw9954
    @lw9954 2 ปีที่แล้ว

    Ty bro

  • @beingzero7541
    @beingzero7541 2 ปีที่แล้ว

    Wow!

  • @SuperStarEevee
    @SuperStarEevee 2 ปีที่แล้ว

    Thank you!

  • @pascal_4732
    @pascal_4732 3 ปีที่แล้ว

    Thank you😄

  • @darkwolf94728
    @darkwolf94728 2 ปีที่แล้ว

    thanks bro! really helped

  • @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.

  • @uuhju7004
    @uuhju7004 3 ปีที่แล้ว

    nice video Bro!

  • @RenovaLabs
    @RenovaLabs 7 หลายเดือนก่อน

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

  • @chuanjiang6931
    @chuanjiang6931 6 หลายเดือนก่อน

    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 2 หลายเดือนก่อน

      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

  • @gustavoaponte1814
    @gustavoaponte1814 ปีที่แล้ว

    meow~! uwu

  • @JoseHernandez-qk5by
    @JoseHernandez-qk5by 11 หลายเดือนก่อน

    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

  • @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.

  • @philtoa334
    @philtoa334 3 ปีที่แล้ว

    : )

  • @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

    • @rozalia6286
      @rozalia6286 ปีที่แล้ว

      I have the same problem

  • @bourenaneayoubabdelmadid5211
    @bourenaneayoubabdelmadid5211 3 ปีที่แล้ว

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

  • @madhubabu-hq4yc
    @madhubabu-hq4yc ปีที่แล้ว

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

  • @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

  • @jhassee
    @jhassee 2 ปีที่แล้ว

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

  • @ryanstephen120
    @ryanstephen120 ปีที่แล้ว

    ta

  • @hahaupscalinh7872
    @hahaupscalinh7872 8 หลายเดือนก่อน

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

  • @RoadToDP
    @RoadToDP 11 หลายเดือนก่อน

    a

  • @Jens1111
    @Jens1111 6 หลายเดือนก่อน

    E

  • @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.

  • @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 ;)

  • @rubenc4696
    @rubenc4696 6 หลายเดือนก่อน

    thanks

  • @cristiucvladimir7909
    @cristiucvladimir7909 6 หลายเดือนก่อน

    thanks bro