Python Advanced Tutorial 5 - MultiThreading

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

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

  • @Number1PKAFan
    @Number1PKAFan 5 лет назад +1

    Best tutorial on youtube

  • @alessandrogerelli
    @alessandrogerelli 6 лет назад

    All your python advanced tutorials are saving my life.

  • @AnubhabHaldarChronum
    @AnubhabHaldarChronum 7 лет назад +12

    This is over 3 years old, and it's in Py2.
    And yet, one of the best Python threading tutorials ever. Well done, my good man. Very helpful, and very well explained.

    • @jd0xt
      @jd0xt 5 лет назад

      Honestly to me I think he doesnt answer any questions in the comments because the code and slides are from the book called Violent Python, even the powerPoint are from Violent Python. he does understand python but the fact that he doesnt explain why he adds each line of code..... for his tone of voice he is reading the code from the book.... I just came here to look some original content about nmap scan in the book Violent Python, because is not about just reading the book, but understanding and he doesnt really explain anything...

  • @nickshavermatson
    @nickshavermatson 8 лет назад

    Very well done. Logical, not too much code, not too little, only exactly as much as is needed to demonstrate the concepts. I wanted to learn python threading in 30 minutes, and your video helped me do just that. Thanks!

    • @DrapsTV
      @DrapsTV  8 лет назад

      +Nick Shaver That's awesome! I'm glad you enjoyed the video! :) Thanks for leaving such a nice comment!

  • @DaaliFilm
    @DaaliFilm 9 лет назад

    Thanks a lot man! I was able to use what I learned here in multiple projects. One of which involved a game timer running in the background, counting down until game over! Well done explaining! :D

    • @DrapsTV
      @DrapsTV  9 лет назад

      CFilm Thanks for your support! glad you found it useful!

  • @collegekid491
    @collegekid491 9 лет назад +3

    Totally agree with nuntius. I watched countless videos and read tutorials trying to learn to use threading. One watch of your video and it clicked. Thanks.

    • @DrapsTV
      @DrapsTV  9 лет назад

      Keith Klassen Glad you found it useful! I had the same experience with threading!

  • @somethingoriginal8603
    @somethingoriginal8603 9 лет назад +2

    THANK YOU! i read so many tutorials online but yours is the only one that i actually understood!

    • @DrapsTV
      @DrapsTV  9 лет назад +1

      Something Original Awesome! :)

  • @kennyPAGC
    @kennyPAGC 7 лет назад +2

    I would love to see more python threading tutorials! more advance stuff!

  • @shawnmclaughlin172
    @shawnmclaughlin172 8 лет назад +1

    Thanks a bunch my dude! Got into Python recently from other languages, your video was a great explanation!

  • @nuntius91
    @nuntius91 9 лет назад +1

    I just wanted to let you know that I've had a really hard time deciphering how to use threading, but this video finally helped me figure it out. Thanks! My program is now at last working.

    • @DrapsTV
      @DrapsTV  9 лет назад

      nuntius91 That's awesome mate! I had a similar problem when I was learning them too. I'm glad I was able to help!

  • @evaneliasyoung
    @evaneliasyoung 9 лет назад +3

    Thank you very much, I had a difficult time learning how to multithread. But found your tutorial very helpful!

    • @DrapsTV
      @DrapsTV  9 лет назад

      +Evan Young That's awesome mate! Thanks for letting me know!

  • @toti5647
    @toti5647 6 лет назад

    Good tutorial on threading.now i can see locks unlocked in my head

  • @darturi2397
    @darturi2397 9 лет назад +1

    Hey Draps! This video just solved my GUI problems. I spent months not knowing what was wrong with it. Halfway thru this video and I already knew how to fix it. Thank you, sir...*subscribed*

  • @planksimonproduktion9399
    @planksimonproduktion9399 6 лет назад

    Sooooo helpful! This is EXACTLY what i needed! Thank you so much, i wish you a great day!

  • @19stanislav03
    @19stanislav03 10 лет назад +1

    Man, your tutorial was amazing!!!

    • @DrapsTV
      @DrapsTV  10 лет назад

      Thanks for the kind words!

  • @davidreynolds9649
    @davidreynolds9649 8 лет назад

    Excellent tutorial, many thanks.

  • @tavycoolish
    @tavycoolish 8 лет назад

    Thanks for the great job. I'm looking to learn python now for a future job in linux sysadmin that will also imply pyscripting. Even tho I have only a basic understanding of the programming languages I really do understand your explanations and again I really appreciate the effort you put into making those videos

  • @vickyrare
    @vickyrare 7 лет назад

    Excellent explanation.

  • @mmatvhindi9362
    @mmatvhindi9362 8 лет назад

    thanks a lot for making this video, you just cleared all the confusion I ever had. Cheers!!!!!

  • @prirsh
    @prirsh 9 лет назад

    Locking demo was easy to understand...thx

  • @WatsitTooyah
    @WatsitTooyah 8 лет назад

    Pretty helpful, thanks! I had a few snags using Python 3 but was fairly easy to resolve.

    • @DrapsTV
      @DrapsTV  8 лет назад

      +Corey Vollmer Thanks! Great work! There is a python3 version of this series! So be sure to check it out if you want to look at more videos in this series.

    • @WatsitTooyah
      @WatsitTooyah 8 лет назад

      +DrapsTV Did not know that, will check it out thanks.

  • @serbuh
    @serbuh 8 лет назад

    Thank you for a clear explanation. Helped me a lot:)

  • @jamesrussell9142
    @jamesrussell9142 10 лет назад +1

    very good info on threading
    we would like more on threading vs multiprocessing

  • @ozgurdede6787
    @ozgurdede6787 7 лет назад

    really nice tutorial !

  • @ANTGPRO
    @ANTGPRO 10 лет назад

    Big Thanks! )))
    Waiting for Networking lesson )))

  • @qakapil
    @qakapil 9 лет назад +1

    Really nice tutorials. You have covered advanced python topics in simple and precise manner.
    Some tutorials on python setuptools and decorators will be really helpful...getting a bit greedy yeah ;)

    • @DrapsTV
      @DrapsTV  9 лет назад

      I'm glad you like them. Hopefully one day I'll get around to writing a tutorial on setuptools :)

  • @kchrisc
    @kchrisc 8 лет назад +1

    Good intro and tutorial.
    Thanks.

  • @aashaydhawal7274
    @aashaydhawal7274 7 лет назад

    Hi Its an awesome video, job well done :) It would be nice if you could come with examples using semaphores,condition and event based locking as well. Hope to see it sooner some time.

  • @johnherschel2278
    @johnherschel2278 7 лет назад

    clearly explation to me, thanks.

  • @THEREIGELADO
    @THEREIGELADO 9 лет назад +1

    Your channel and really fantastic, won a Brazileiro registered and tanned on your video!

    • @DrapsTV
      @DrapsTV  9 лет назад +1

      Thanks! I'm glad you enjoy it!

  • @kailashadvani2326
    @kailashadvani2326 10 лет назад

    excellent review

  • @vivekanandmishra7775
    @vivekanandmishra7775 8 лет назад

    Thanks bro! This helped

  • @Vegito_Blue2
    @Vegito_Blue2 7 лет назад

    Dude thanks for this tutorial! This is what I exactly wanted. I noticed steam icon on the taskbar, what games do you play or can I share your username?

  • @badbond420
    @badbond420 8 лет назад +2

    Thanks a lot, such a clear explaination.

    • @DrapsTV
      @DrapsTV  8 лет назад

      +bibek mantree Thanks mate!

  • @shivam-garg
    @shivam-garg 9 лет назад +1

    I have learnt a lot through your videos. thanks.
    I have a problem.
    I have 3 computer nodes (A, B, C). I want to send a file from A to C via B. Whatever packets node B is getting from A should get transferred directly to node C (if C is connected to B). I have been told that my problem could be solved using synchronous threading but I do not know how. Please help me out.

    • @DrapsTV
      @DrapsTV  9 лет назад

      Shivam Garg
      Threading may be useful. Are you familiar with Python Networking? Well B must know about A & C so you should be able to open two sockets, one connecting to A and one connecting to C. Then just pass the information along:
      tempData = socketA.recv(2048) #receive from A
      #maybe store data?, count it?
      socketC.send(tempData) #send on to C
      Using threading, you may have a socket open in each thread. and pass the data between the threads one sending and another receiving. If you take this approach, using a queue for storing the data would be a wise move to avoid thread access issue.

  • @jafferwilson1633
    @jafferwilson1633 7 лет назад

    can you please explain multi threading and multi processing using python program, taing an example with sorting and removing duplicates from bigger files?

  • @ammebr
    @ammebr 7 лет назад

    Hi Draps TV, hoe can watch your remaining advanced training videos, i cant find it on your channel

  • @Pterry23real
    @Pterry23real 10 лет назад +4

    A little tipp for your terminal work in ubuntu or every bash shell. If you add in the first line of your file "#! /usr/bin/python" your systems knows wich interpreter or compiler it has to use, important if you have to use different versions etc. After this you can make your file exectutable with "chmod +x file". After this you can execute the file with "/file".
    This is not necessary for your kind of tutorials but "if __name__ == "__main__:" is also not realy necessary in your short programs ;)

  • @syanure21111
    @syanure21111 10 лет назад +1

    Awesome Tutos :D

  • @AshutoshPandey-se8vt
    @AshutoshPandey-se8vt 6 лет назад

    Sir when we use locks our program will become slow..as we can only use thread at a time then what's the point of using threads when we are executing just a single thread in our program

  • @ravitejanama5058
    @ravitejanama5058 7 лет назад

    What is the use of background.join()?When I place it after background.start() it's first writing into the file and then printing 200.If I place it after print it's first printing and then writing into the the file.Can you tell me what is the difference?what is the difference between start and run in the above code?

  • @tinasky823
    @tinasky823 9 лет назад +5

    It is awesome!I want to learn more knowledges about this topic.Thanks for your share. :)

  • @xiaofeiguo2091
    @xiaofeiguo2091 8 лет назад +1

    Great tutorial. One suggestion: You can just use print name + ": " + str(time.ctime()) instead of print name + ": " + str(time.ctime(time.time()))

  • @rajatjain7465
    @rajatjain7465 7 лет назад

    while we were using locks to print timer1 and timer2, shouldn't the order of the printed statements be:
    Timer: Timer1 has started
    timer1 has acquired lock
    then printing the 5 line consisting of time
    timer1 has released lock
    Timer: timer2 has started
    timer2 has acquired lock
    then printing the 5 line consisting of time
    timer2 has released lock
    main is completed
    i was expecting this output bcz once the thread t1was printed, the statement "timer: timer2 has started"
    Plz explain

  • @PurnachandMedisetty
    @PurnachandMedisetty 6 лет назад

    great; thank you. at 22:24 timer-1 started at 17:41:56 and about the same time acquired the lock, then quickly afterwards Main completed and timer-2 started at about the same time. 41:56, first 5 times output from timer-1 are :57 58 59 60 01 so does that mean all this while timer-2 waiting for lock to be released? then timer-2 started to output at :03 05 07 09 11. I am confused if anything being processed in parallel. I am beginner, hopefully I will get is some day. Your tutorials have been quite helpful.

  • @ashishgehani2045
    @ashishgehani2045 9 лет назад +1

    Hi, as I can see from the code you typed in for acquiring lock and releasing them just to make sure that print function in python should be accessible by only one thread, you were acquiring lonk after printing one line in your timer() function. Don't you think acquiring lock before printing first line in timer() function would be an ideal way?

    • @DrapsTV
      @DrapsTV  9 лет назад

      Yeah, you really only want one thread managing your output. With the last example I was trying to make it as obvious as possible for people to see how locks work. You will see that both threads start, but one obtains the lock, then the other thread has to wait.

  • @o0O0oLOLo0O0o
    @o0O0oLOLo0O0o 7 лет назад

    if I may ask,
    in this video, what is the purpose of the lock?
    you can as well just call timer two times without the lock.
    Are there other applications?

    • @DrapsTV
      @DrapsTV  7 лет назад

      The lock in this video is just an example of what a Lock does. If you ever have an issue where two threads are interacting with shared data and issues are arising because both threads are writing at the same time or reading out of sync. Then locks are going to help you out.

  • @DrapsTV
    @DrapsTV  10 лет назад +3

    Thanks! :D

  • @radarmusen
    @radarmusen 9 лет назад

    Thanks, very helpfull video. Will try it on a raspberry pi.

  • @Amine-wp8bm
    @Amine-wp8bm 10 лет назад

    You Are Awesome Man ! can you make videos about the basics of python?

    • @DrapsTV
      @DrapsTV  10 лет назад

      I do have a beginner series that can get you up and running, There is also an Intermediate series too :)
      Python Beginner Tutorial 1 - Intro & Setup

    • @Amine-wp8bm
      @Amine-wp8bm 10 лет назад

      Thanks

  • @dantecavallin8229
    @dantecavallin8229 8 лет назад

    perfect! thanks!

  • @tiagovla
    @tiagovla 7 лет назад

    It makes no sense to me saying that if I have 10 threads my processing will be 10 times faster. Doesn't it depend on if the process is intensive or not and on the number of CPUs I have? To me, If I have only one CPU and my process takes 10s to run, it will still get 20s to run the same code twice with 2 threads, but they will finish at the same time.

  • @samstutorials1611
    @samstutorials1611 9 лет назад

    I have a question:
    Can I use Async MultiThreading to write in a file and read from this file while we are still writing in it
    Thank you!

    • @DrapsTV
      @DrapsTV  9 лет назад +1

      +Sami Kanafani It's a little unpredictable to do that. it's also platform independent. generally you want to make sure a program has closed and finished with the file before your open it again to read or write.

    • @samstutorials1611
      @samstutorials1611 9 лет назад

      +DrapsTV Thank you

  • @matteo7moh
    @matteo7moh 9 лет назад +1

    Thanks for the tutorials!
    Are you from England?

    • @DrapsTV
      @DrapsTV  9 лет назад

      +matteo7moh You're welcome! Nah, I'm from Australia!

  • @natecampbell7704
    @natecampbell7704 9 лет назад

    When I try the simple timer example I don't get timer2 starting until timer1 finishes. The two threads don't seem to be running in parallel but sequentially. Is there something I'm missing? Is there some way I need to enable threading on my machine? I've tried using Ubuntu and CentOS and get the same result (both have multi-core cpus)

    • @DrapsTV
      @DrapsTV  9 лет назад

      Did you use the example 3 code? Because there we used Threading *Locks* to lock onto timer1 so that timer2 has to wait for timer1 has to finish (To easily teach what locks are doing). Locks are used to wait for data to be safe to access. Example 1 and Example 2 should provide threaded processing.

    • @natecampbell7704
      @natecampbell7704 9 лет назад

      Thanks for the reply. I figured out the problem. Example one was giving me syntax errors so I "fixed" them so the script would run. In doing so I called Thread incorrectly. After some Googling I figued out the error and I got it to work as intended. thanks again.

  • @sayantansarkar4409
    @sayantansarkar4409 5 лет назад

    So basically the lock is making the thread act as a sequential execution, then why use thread !?

  • @vSemaaj
    @vSemaaj 9 лет назад +1

    in your second tut how did the thread know its target was self.run()?

    • @DrapsTV
      @DrapsTV  9 лет назад +2

      James The Thread Base class calls run() when start() is called. so when we create our own subclass we get all of the base class methods. we just override what the run method does.

  • @ANTGPRO
    @ANTGPRO 10 лет назад

    I hope you will show how to work with sockets ))
    Thanks! ))

    • @DrapsTV
      @DrapsTV  10 лет назад +1

      Yep That's the next video :), Hoping to have it out next week, busy with Easter stuff at the moment :)

    • @ANTGPRO
      @ANTGPRO 10 лет назад

      Thanks! )))))

  • @henkhenkeinstein4737
    @henkhenkeinstein4737 10 лет назад +1

    Thank you

  • @victorzedwings
    @victorzedwings 5 лет назад

    why tLock is not imported into function as global?

  • @anupkeshwani2628
    @anupkeshwani2628 8 лет назад

    How does the 'run' function in the asyncWrite class runs without specifying the 'target' while creating the thread?

    • @DrapsTV
      @DrapsTV  8 лет назад

      the asyncWrite class is a thread, it inherits from the thread base class. So instead of using the threading.Thread generic class we create a custom class that has a specific purpose.

    • @anupkeshwani2628
      @anupkeshwani2628 8 лет назад

      I got it. Thanks for such simple explanatory video.

  • @snowbunniez4030
    @snowbunniez4030 9 лет назад +1

    Thank you for this tutorial, I got my stuff working, having the GUI freeze up when the program is busy is pretty...... ehh....

    • @DrapsTV
      @DrapsTV  9 лет назад

      That's awesome to hear! Yeah freezing GUI isn't very cool...

  • @Radobilly
    @Radobilly 8 лет назад

    Liked, commented and subscribed (y)

  • @ArnobAlmazee
    @ArnobAlmazee 6 лет назад

    amazing one .. thanks for share ... :)

  • @moog500
    @moog500 9 лет назад

    seems really similar to creating instantiating a new object. Am i right?

    • @moog500
      @moog500 9 лет назад

      +moog500 didn't finish the video; I am stupid

    • @DrapsTV
      @DrapsTV  9 лет назад

      +moog500 You are far from stupid mate!

  • @deeproy7292
    @deeproy7292 7 лет назад

    can you tell me how to learn networks from scratch

  • @jacheto
    @jacheto 8 лет назад

    i find scary that your seconds are equal to my seconds in the timer lol

    • @DrapsTV
      @DrapsTV  8 лет назад +3

      *X-Files soundtrack*

  • @jamesmccoy8568
    @jamesmccoy8568 9 лет назад +1

    My program doesn't end when I run the first code example. Anyone know what the problem might be?
    I'm using Python 2.5 on Mac with the following code:
    from threading import Thread
    import time
    def timer(name, delay, repeat):
    print "Timer: " + name + " started"
    # Run for amount of times given in repeat argument
    while repeat > 0:
    # Sleep for amount stated in delay argument
    time.sleep(delay)
    print name + ": " + str(time.ctime(time.time()))
    repeat -= 1
    print "Timer " + name + " completed"
    def Main():
    # Thread 1 # function #func arguments ### Creates Thread object
    t1 = Thread(target = timer, args = ("Timer 1", 1, 5))
    t2 = Thread(target = timer, args = ("Timer 2", 2, 5))
    # Method that starts the thread
    t1.start()
    t2.start()
    # Will show when Main function completes
    print "Main Complete"
    if __name__ == '__main__':
    Main()

    • @DrapsTV
      @DrapsTV  9 лет назад

      +James McCoy How did you go mate? Did you figure out what was wrong? What kind of output where you getting?

    • @jamesmccoy8568
      @jamesmccoy8568 9 лет назад +2

      +DrapsTV Hey, before it was just giving me a cursor instead of going to the next shell line. I had to use keyboard interrupt to stop the program. But I ended up running the program from my bash terminal and it worked fine. Apparently it's an IDLE thing. I should really stop using IDLE...

    • @davidreynolds9649
      @davidreynolds9649 8 лет назад

      and 2.5 !

    • @aggx
      @aggx 7 лет назад

      I ran into the same issue with IDLE (3.5). I'm guessing it's the IDLE interpreter thread that is getting hung.

    • @jamesmccoy8568
      @jamesmccoy8568 7 лет назад

      Now it's been so long I forget how I fixed it...

  • @mayukh_
    @mayukh_ 10 лет назад +2

    show an example on semaphore too please..:)

    • @DrapsTV
      @DrapsTV  10 лет назад +1

      Semaphores are the same as locks however you can specify the number of threads that can access data before it's locked until another thread releases one. if I had three threads and a semaphore with a value of two, the third thread to try access the data would have to wait until thread 1 or 2 is finished with it first.

  • @udayy4822
    @udayy4822 7 лет назад

    In the 2nd program can we do background=Thread(target=AysncWrite,args=(message,'out.txt') instead

  • @holographicsol2747
    @holographicsol2747 8 лет назад

    thanks :D

  • @redsddd9876
    @redsddd9876 9 лет назад

    why d not use windows python ??

  • @kundan_28
    @kundan_28 7 лет назад

    you shouldn't have used the time module in the example

  • @nikolaykorablin1401
    @nikolaykorablin1401 7 лет назад +1

    damn tabs.. but video is good :)

  • @sanshinron
    @sanshinron 9 лет назад

    Vim? Seriously? Dude, use PyCharm or VS with PTVS...

  • @jean-fredericfontaine2695
    @jean-fredericfontaine2695 6 лет назад

    Multithreading is done by concurency. Nothing is parralel here..

  • @nlreeftank
    @nlreeftank 10 лет назад +2

    sjeez use .format

    • @HarmjanJeePsteRiouS
      @HarmjanJeePsteRiouS 10 лет назад +1

      Hoever ben je met python, ik kan met ableton live wel hulp gebruiken. Het scripten voor mijn apc40 mpoet in python namelijk.

    • @DrapsTV
      @DrapsTV  10 лет назад

      I haven't done any complex formatting here so I wouldn't say it was necessary.

    • @nlreeftank
      @nlreeftank 10 лет назад

      vrij ver

    • @nlreeftank
      @nlreeftank 10 лет назад

      format is always necessary :P

    • @DrapsTV
      @DrapsTV  10 лет назад +2

      format is not always necessary. It's slow. I would never use it when something needs to be fast. :P