title
Python 3 Programming Tutorial - Threading module

description
In this Python 3 programming tutorial video, we cover the threading module that is a part of the standard library. With this, you can engage in threading, which allows multiple processes to take place at a time. Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/ Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

detail
{'title': 'Python 3 Programming Tutorial - Threading module', 'heatmap': [{'end': 442.418, 'start': 387.05, 'weight': 0.861}, {'end': 798.565, 'start': 762.47, 'weight': 0.702}, {'end': 990.628, 'start': 930.624, 'weight': 0.745}], 'summary': 'The tutorial covers python threading basics, emphasizing the importance of locks to prevent data corruption, and demonstrates multithreading with 10 worker threads performing tasks concurrently, resulting in a total execution time of around one second for 20 jobs.', 'chapters': [{'end': 319.249, 'segs': [{'end': 43.618, 'src': 'embed', 'start': 19.537, 'weight': 0, 'content': [{'end': 27.06, 'text': 'is very linear and it would be nice to do some parallel tasks from time to time and use a little bit more of your processing power.', 'start': 19.537, 'duration': 7.523}, {'end': 30.611, 'text': "Let's go ahead and get started with threading.", 'start': 28.63, 'duration': 1.981}, {'end': 37.275, 'text': "So with threading, it's a part of your standard library, so you don't have to actually download anything or install anything.", 'start': 31.231, 'duration': 6.044}, {'end': 40.056, 'text': 'You already did it when you installed Python 3.', 'start': 37.315, 'duration': 2.741}, {'end': 43.618, 'text': "So let's go ahead and import threading, and that's the name of the module.", 'start': 40.056, 'duration': 3.562}], 'summary': 'Threading in python allows parallel tasks without extra installation.', 'duration': 24.081, 'max_score': 19.537, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA19537.jpg'}, {'end': 169.739, 'src': 'embed', 'start': 140.696, 'weight': 1, 'content': [{'end': 143.798, 'text': 'And so this causes problems when you have shared variables because.', 'start': 140.696, 'duration': 3.102}, {'end': 151.364, 'text': 'There might be double modification, or not double modification, but really only one of the modifications gets passed through.', 'start': 145.92, 'duration': 5.444}, {'end': 155.748, 'text': "So anyway, it's pretty much integral.", 'start': 152.786, 'duration': 2.962}, {'end': 158.33, 'text': 'So you might see a lot of threading tutorials.', 'start': 155.788, 'duration': 2.542}, {'end': 162.794, 'text': 'My first threading tutorial was a great example where we did not account for this.', 'start': 158.37, 'duration': 4.424}, {'end': 164.875, 'text': 'And this is kind of a major issue.', 'start': 162.894, 'duration': 1.981}, {'end': 169.739, 'text': "I think that it's kind of silly, like you would do threading and not use this.", 'start': 164.916, 'duration': 4.823}], 'summary': 'Shared variable issues in threading can cause single modification problems.', 'duration': 29.043, 'max_score': 140.696, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA140696.jpg'}, {'end': 268.47, 'src': 'embed', 'start': 222.04, 'weight': 3, 'content': [{'end': 233.745, 'text': 'So if you know a thread tries to access a locked variable, it will sit there and wait, sort of like a queue, Until that variable becomes unlocked.', 'start': 222.04, 'duration': 11.705}, {'end': 238.288, 'text': 'then it will lock it back up, do what it needs to do, then unlock it and move on now.', 'start': 233.745, 'duration': 4.543}, {'end': 243.871, 'text': 'Describing this process takes a little bit of time, but actually this happens very quickly.', 'start': 238.288, 'duration': 5.583}, {'end': 247.013, 'text': "So you're not really sacrificing too much time doing this.", 'start': 243.871, 'duration': 3.142}, {'end': 249.395, 'text': "but it's absolutely necessary if you have shared variables.", 'start': 247.013, 'duration': 2.382}, {'end': 253.838, 'text': "So the first thing that we want to do is we're gonna, we're gonna use a lock on printing.", 'start': 249.395, 'duration': 4.443}, {'end': 260.322, 'text': "So we're gonna say print underscore lock equals threading dot lock.", 'start': 253.838, 'duration': 6.484}, {'end': 266.027, 'text': "okay, so we're referencing the locking ability of the threading module and print lock.", 'start': 260.322, 'duration': 5.705}, {'end': 268.47, 'text': 'basically what you have to have is you have to have a lock per thing.', 'start': 266.027, 'duration': 2.443}], 'summary': 'Threading utilizes locks to ensure quick and necessary access to shared variables, using one lock per thing.', 'duration': 46.43, 'max_score': 222.04, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA222040.jpg'}, {'end': 328.226, 'src': 'embed', 'start': 301.07, 'weight': 5, 'content': [{'end': 306.813, 'text': 'and so it kind of converged the two and and even though this was, you know, not like a catastrophic thing, that they were mixed it.', 'start': 301.07, 'duration': 5.743}, {'end': 312.84, 'text': "it was a perfect example of what will happen and you'll get data corruption basically like that.", 'start': 307.373, 'duration': 5.467}, {'end': 314.823, 'text': 'um, so anyway, print lock.', 'start': 312.84, 'duration': 1.983}, {'end': 319.249, 'text': "we're going to use this to lock printing now.", 'start': 314.823, 'duration': 4.426}, {'end': 328.226, 'text': "um, trying to think of what order i want to start with, but First of all, we want to kind of we're going to try to visualize what we're going to do.", 'start': 319.249, 'duration': 8.977}], 'summary': 'Convergence of two systems led to data corruption, prompting implementation of print lock for visualizing the process.', 'duration': 27.156, 'max_score': 301.07, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA301070.jpg'}], 'start': 0.189, 'title': 'Python threading basics and importance of using locks', 'summary': 'Covers the basics of threading in python, highlighting the importance of avoiding shared variables and demonstrating the potential issues when multiple tasks access the same variable simultaneously, using skittles as an example. it also emphasizes the integral use of locks in threading to prevent data corruption when dealing with shared variables, with the example of using a print lock in a threading tutorial and the potential consequences of not using it.', 'chapters': [{'end': 140.696, 'start': 0.189, 'title': 'Python threading basics', 'summary': 'Covers the basics of threading in python, highlighting the importance of avoiding shared variables and demonstrating the potential issues when multiple tasks access the same variable simultaneously, using skittles as an example.', 'duration': 140.507, 'highlights': ['The concept of threading in Python is introduced, emphasizing the ability to perform multiple tasks simultaneously, utilizing the processing power more efficiently.', 'The potential issue of shared variables in threading is explained through an example involving the simultaneous modification of a variable by multiple threads, highlighting the incorrectness of the results due to concurrent updates.', 'The need to handle shared variables carefully in threading to avoid conflicting updates and ensure accurate results is underscored, demonstrating the importance of managing concurrent access to shared resources.']}, {'end': 319.249, 'start': 140.696, 'title': 'Importance of using locks in threading', 'summary': 'Emphasizes the integral use of locks in threading to prevent data corruption when dealing with shared variables, with the example of using a print lock in a threading tutorial and the potential consequences of not using it.', 'duration': 178.553, 'highlights': ['The importance of using locks in threading to prevent data corruption when dealing with shared variables is emphasized, with the example of using a print lock in a threading tutorial. The chapter highlights the significance of using locks in threading to avoid data corruption with shared variables, using the example of a print lock in a threading tutorial to illustrate the potential consequences of not using it.', 'Describing the process of locking and unlocking variables for thread safety is explained, emphasizing the necessity of using locks with shared variables. The process of locking and unlocking variables for thread safety is explained, stressing the necessity of using locks when dealing with shared variables to ensure data integrity and prevent data corruption.', 'The potential consequences of not using locks in threading, leading to data corruption and overlapping output, are highlighted with the example of a jumbled mess of text in a print function due to lack of synchronization. The transcript highlights the potential consequences of not using locks in threading, leading to data corruption and overlapping output, with the example of a jumbled mess of text in a print function due to lack of synchronization, illustrating the importance of using locks to avoid such issues.']}], 'duration': 319.06, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA189.jpg', 'highlights': ['The concept of threading in Python is introduced, emphasizing the ability to perform multiple tasks simultaneously, utilizing the processing power more efficiently.', 'The potential issue of shared variables in threading is explained through an example involving the simultaneous modification of a variable by multiple threads, highlighting the incorrectness of the results due to concurrent updates.', 'The need to handle shared variables carefully in threading to avoid conflicting updates and ensure accurate results is underscored, demonstrating the importance of managing concurrent access to shared resources.', 'The importance of using locks in threading to prevent data corruption when dealing with shared variables is emphasized, with the example of using a print lock in a threading tutorial.', 'Describing the process of locking and unlocking variables for thread safety is explained, emphasizing the necessity of using locks with shared variables.', 'The potential consequences of not using locks in threading, leading to data corruption and overlapping output, are highlighted with the example of a jumbled mess of text in a print function due to lack of synchronization.']}, {'end': 609.793, 'segs': [{'end': 346.273, 'src': 'embed', 'start': 319.249, 'weight': 4, 'content': [{'end': 328.226, 'text': "um, trying to think of what order i want to start with, but First of all, we want to kind of we're going to try to visualize what we're going to do.", 'start': 319.249, 'duration': 8.977}, {'end': 332.947, 'text': "So what I'd like to do is like have, you know, we go, you want to define a worker.", 'start': 328.326, 'duration': 4.621}, {'end': 336.469, 'text': 'You want to define how many workers we have or how many threads we have.', 'start': 333.168, 'duration': 3.301}, {'end': 338.83, 'text': 'And then what do you want those threads to do??', 'start': 337.289, 'duration': 1.541}, {'end': 346.273, 'text': "So I kind of think that what we'll do first is we're going to say Q.", 'start': 339.57, 'duration': 6.703}], 'summary': 'Planning to define worker, threads, and tasks for the project.', 'duration': 27.024, 'max_score': 319.249, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA319249.jpg'}, {'end': 394.675, 'src': 'embed', 'start': 365.84, 'weight': 2, 'content': [{'end': 370.162, 'text': "Now, depending on what kind of work you're going to do, so the workers all have the same working function here.", 'start': 365.84, 'duration': 4.322}, {'end': 372.763, 'text': 'They might have different working functions.', 'start': 371.102, 'duration': 1.661}, {'end': 376.544, 'text': 'And you might still start with for x in range of 10 because you have 10 threads.', 'start': 372.803, 'duration': 3.741}, {'end': 380.085, 'text': 'And then you might have something that picks the function that it runs specifically.', 'start': 376.924, 'duration': 3.161}, {'end': 381.226, 'text': "But for now, we're just trying to.", 'start': 380.125, 'duration': 1.101}, {'end': 386.529, 'text': 'Keep this as simple as possible, but yet as useful as possible as far as education is concerned.', 'start': 381.966, 'duration': 4.563}, {'end': 394.675, 'text': "So we're going to say for x in range of 10, t is going to equal threading.capitalThread.", 'start': 387.05, 'duration': 7.625}], 'summary': 'Workers have different functions, with 10 threads for various tasks. aim for simplicity and usefulness in education.', 'duration': 28.835, 'max_score': 365.84, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA365840.jpg'}, {'end': 442.418, 'src': 'heatmap', 'start': 387.05, 'weight': 0.861, 'content': [{'end': 394.675, 'text': "So we're going to say for x in range of 10, t is going to equal threading.capitalThread.", 'start': 387.05, 'duration': 7.625}, {'end': 400.399, 'text': "And then we're going to say our target is going to equal threader.", 'start': 395.435, 'duration': 4.964}, {'end': 405.742, 'text': 'Okay So these are just for defining our threads and the target of our threads.', 'start': 400.419, 'duration': 5.323}, {'end': 407.363, 'text': 'Let me make some more space here.', 'start': 405.782, 'duration': 1.581}, {'end': 407.904, 'text': 'Okay Okay.', 'start': 407.383, 'duration': 0.521}, {'end': 415.195, 'text': "and now what we're going to do is we're going to see t dot daemon, and then we're going to say that equals true.", 'start': 408.963, 'duration': 6.232}, {'end': 415.415, 'text': "so we're.", 'start': 415.195, 'duration': 0.22}, {'end': 418.889, 'text': 'Why is it not turning yellow??', 'start': 417.008, 'duration': 1.881}, {'end': 421.93, 'text': "So we're classifying this as a demon.", 'start': 420.51, 'duration': 1.42}, {'end': 426.312, 'text': "And so basically what's going to happen is it will die when the main thread dies.", 'start': 422.31, 'duration': 4.002}, {'end': 429.613, 'text': "So that's all we're doing there.", 'start': 428.093, 'duration': 1.52}, {'end': 437.156, 'text': "Now, the next thing that we want to do is after you've done your demon definition, then we can do t.start.", 'start': 430.174, 'duration': 6.982}, {'end': 440.037, 'text': "So we start the threading after we've defined it.", 'start': 437.336, 'duration': 2.701}, {'end': 442.418, 'text': "So we couldn't do it until we said what it was.", 'start': 440.077, 'duration': 2.341}], 'summary': 'Defining and starting a daemon thread for threading.capitalthread with target threader.', 'duration': 55.368, 'max_score': 387.05, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA387050.jpg'}, {'end': 437.156, 'src': 'embed', 'start': 408.963, 'weight': 3, 'content': [{'end': 415.195, 'text': "and now what we're going to do is we're going to see t dot daemon, and then we're going to say that equals true.", 'start': 408.963, 'duration': 6.232}, {'end': 415.415, 'text': "so we're.", 'start': 415.195, 'duration': 0.22}, {'end': 418.889, 'text': 'Why is it not turning yellow??', 'start': 417.008, 'duration': 1.881}, {'end': 421.93, 'text': "So we're classifying this as a demon.", 'start': 420.51, 'duration': 1.42}, {'end': 426.312, 'text': "And so basically what's going to happen is it will die when the main thread dies.", 'start': 422.31, 'duration': 4.002}, {'end': 429.613, 'text': "So that's all we're doing there.", 'start': 428.093, 'duration': 1.52}, {'end': 437.156, 'text': "Now, the next thing that we want to do is after you've done your demon definition, then we can do t.start.", 'start': 430.174, 'duration': 6.982}], 'summary': 'Configuring t dot daemon as true, and starting t thread.', 'duration': 28.193, 'max_score': 408.963, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA408963.jpg'}, {'end': 494.008, 'src': 'embed', 'start': 468.94, 'weight': 0, 'content': [{'end': 476.262, 'text': 'And the idea here is to see how much time it took versus how much time it should have took if it was programmed in parallel,', 'start': 468.94, 'duration': 7.322}, {'end': 479.603, 'text': 'or I mean if it was programmed linearly talk.', 'start': 476.262, 'duration': 3.341}, {'end': 482.084, 'text': "So now what we're gonna do is assign some jobs.", 'start': 480.123, 'duration': 1.961}, {'end': 487.106, 'text': "So we're gonna say for worker in range of 20.", 'start': 482.124, 'duration': 4.982}, {'end': 490.907, 'text': 'So what this does is it just says for whatever variable we want.', 'start': 487.106, 'duration': 3.801}, {'end': 494.008, 'text': "So we're calling that variable worker in range 20.", 'start': 490.987, 'duration': 3.021}], 'summary': 'Analyzing time taken vs. expected time for parallel vs. linear programming.', 'duration': 25.068, 'max_score': 468.94, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA468940.jpg'}, {'end': 609.793, 'src': 'embed', 'start': 550.578, 'weight': 1, 'content': [{'end': 559.086, 'text': 'actually what we need to do is time dot time, minus start, and that will print out you know how many seconds.', 'start': 550.578, 'duration': 8.508}, {'end': 563.01, 'text': 'basically this this whole process took.', 'start': 559.086, 'duration': 3.924}, {'end': 576.246, 'text': "Now, a couple of things that we need to do here is, first of all, we've got everything set up, basically how we want to organize this queue.", 'start': 563.999, 'duration': 12.247}, {'end': 586.413, 'text': "We've got 20 jobs that we want to do, and we've got 10 workers that will work on this job.", 'start': 576.567, 'duration': 9.846}, {'end': 593.371, 'text': "Now what we want to do is we've got Q equals Q.", 'start': 588.28, 'duration': 5.091}, {'end': 595.716, 'text': "Above this, we'll do.", 'start': 593.371, 'duration': 2.345}, {'end': 601.829, 'text': "define and we're going to say our example job.", 'start': 598.547, 'duration': 3.282}, {'end': 602.99, 'text': 'and basically this is you know,', 'start': 601.829, 'duration': 1.161}, {'end': 609.793, 'text': 'what job are we actually going to have these workers do and the parameter that will pass through is worker for the example job.', 'start': 602.99, 'duration': 6.803}], 'summary': 'Process took time.time - start, with 20 jobs and 10 workers.', 'duration': 59.215, 'max_score': 550.578, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA550578.jpg'}], 'start': 319.249, 'title': 'Python multithreading', 'summary': 'Discusses implementing multithreading in python with 10 worker threads performing tasks concurrently, and measuring performance time for 20 jobs with 10 workers.', 'chapters': [{'end': 452.544, 'start': 319.249, 'title': 'Multithreading in python', 'summary': 'Discusses the implementation of multithreading in python, defining 10 worker threads to perform tasks concurrently and setting them as daemons to terminate with the main thread.', 'duration': 133.295, 'highlights': ['The chapter discusses defining 10 worker threads to perform tasks concurrently.', 'The chapter explains setting the worker threads as daemons to terminate with the main thread.', 'The chapter emphasizes the importance of visualizing the process and simplifying it for educational purposes.']}, {'end': 609.793, 'start': 452.544, 'title': 'Python thread performance analysis', 'summary': 'Explains how to measure the execution time of a process with python threads, organizing 20 jobs for 10 workers and analyzing the time taken for performance calculations.', 'duration': 157.249, 'highlights': ['The chapter discusses measuring the time taken for performance calculations, including organizing 20 jobs for 10 workers and analyzing the execution time of the process with Python threads.', 'The process involves assigning 20 instances for the workers and putting them to work in a queue, followed by waiting for the threads to terminate and printing the time taken for the entire job to complete.', 'The chapter explains the setup of organizing 20 jobs for 10 workers and defining the example job to be completed by the workers with the parameter passed as worker.']}], 'duration': 290.544, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA319249.jpg', 'highlights': ['The chapter discusses measuring the time taken for performance calculations, including organizing 20 jobs for 10 workers and analyzing the execution time of the process with Python threads.', 'The process involves assigning 20 instances for the workers and putting them to work in a queue, followed by waiting for the threads to terminate and printing the time taken for the entire job to complete.', 'The chapter discusses defining 10 worker threads to perform tasks concurrently.', 'The chapter explains setting the worker threads as daemons to terminate with the main thread.', 'The chapter emphasizes the importance of visualizing the process and simplifying it for educational purposes.', 'The chapter explains the setup of organizing 20 jobs for 10 workers and defining the example job to be completed by the workers with the parameter passed as worker.']}, {'end': 1121.973, 'segs': [{'end': 671.153, 'src': 'embed', 'start': 609.793, 'weight': 1, 'content': [{'end': 616.937, 'text': 'and all this example job is going to do is some really heavy processing that just so happens to take time, dot sleep, uh, 0.5.', 'start': 609.793, 'duration': 7.144}, {'end': 621.54, 'text': 'so half a second for this, this job, to complete.', 'start': 616.937, 'duration': 4.603}, {'end': 628.657, 'text': "and then what we're going to do is we're going to say with print lock, And so it's gonna with this lock, what is it gonna do??", 'start': 621.54, 'duration': 7.117}, {'end': 632.759, 'text': "It's gonna perform this operation and the operation is gonna be.", 'start': 628.697, 'duration': 4.062}, {'end': 642.122, 'text': "it's gonna be print, but you can't perform this operation without, while this lock is locked basically.", 'start': 632.759, 'duration': 9.363}, {'end': 659.937, 'text': "We're gonna print, and we'll print threading, whoops, threading.current underscore thread dot name, And then we'll print worker.", 'start': 642.142, 'duration': 17.795}, {'end': 665.67, 'text': "So this will print the current threader's name, basically, and the worker.", 'start': 660.98, 'duration': 4.69}, {'end': 670.232, 'text': "then, when it's all done with this, it will release that print lock.", 'start': 667.771, 'duration': 2.461}, {'end': 671.153, 'text': 'okay, so we can.', 'start': 670.232, 'duration': 0.921}], 'summary': 'Example job with heavy processing takes 0.5 seconds to complete and prints thread name and worker.', 'duration': 61.36, 'max_score': 609.793, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA609793.jpg'}, {'end': 798.565, 'src': 'heatmap', 'start': 762.47, 'weight': 0.702, 'content': [{'end': 764.211, 'text': 'So example job worker.', 'start': 762.47, 'duration': 1.741}, {'end': 775.678, 'text': "and then finally we're going to go q.task, underscore done and then basically we're saying this thread is complete and we're ready to move on.", 'start': 764.211, 'duration': 11.467}, {'end': 778.826, 'text': 'Okay? So a lot of code.', 'start': 776.403, 'duration': 2.423}, {'end': 784.353, 'text': "Before I go and explain it, let's go ahead and run it or do it like one more explanation real quick through a summary.", 'start': 779.727, 'duration': 4.626}, {'end': 785.975, 'text': "But let's go ahead and run it first.", 'start': 784.793, 'duration': 1.182}, {'end': 787.116, 'text': 'Come over here.', 'start': 786.596, 'duration': 0.52}, {'end': 792.222, 'text': 'And we get thread 9, all the threads basically that ran.', 'start': 788.18, 'duration': 4.042}, {'end': 796.264, 'text': 'And then we get the job number basically, the work job number.', 'start': 792.242, 'duration': 4.022}, {'end': 798.565, 'text': 'So we have 10 threads total.', 'start': 796.604, 'duration': 1.961}], 'summary': 'Code runs 10 threads, completing job worker tasks.', 'duration': 36.095, 'max_score': 762.47, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA762470.jpg'}, {'end': 850.801, 'src': 'embed', 'start': 818.701, 'weight': 0, 'content': [{'end': 819.702, 'text': 'So we can close this.', 'start': 818.701, 'duration': 1.001}, {'end': 823.125, 'text': "Now let's go ahead and talk about why this is so important.", 'start': 820.522, 'duration': 2.603}, {'end': 827.649, 'text': 'So we had, again, we had 10 threads, 20 jobs.', 'start': 823.925, 'duration': 3.724}, {'end': 829.951, 'text': 'Each job took 0.5 seconds.', 'start': 828.269, 'duration': 1.682}, {'end': 842.897, 'text': 'So normally this should have taken with 20 tasks to do at 0.5 seconds each, should have taken 10 seconds total to perform this operation.', 'start': 832.853, 'duration': 10.044}, {'end': 846.899, 'text': 'But as we can see, it actually took more like one second.', 'start': 843.398, 'duration': 3.501}, {'end': 850.801, 'text': 'Now, why did it take one second? Well, we had 20 jobs and 10 workers.', 'start': 847.259, 'duration': 3.542}], 'summary': 'Despite 20 jobs at 0.5 sec, 10 workers took 1 sec, not 10 sec, in operation.', 'duration': 32.1, 'max_score': 818.701, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA818701.jpg'}, {'end': 897.373, 'src': 'embed', 'start': 870.934, 'weight': 8, 'content': [{'end': 878.84, 'text': "so that's threading okay, and that's why threading is so powerful and useful is because, while we have one thread idle,", 'start': 870.934, 'duration': 7.906}, {'end': 881.462, 'text': 'we can actually be doing work with another thread.', 'start': 878.84, 'duration': 2.622}, {'end': 883.883, 'text': "um, but of course it's necessary to have the locks and all of that.", 'start': 881.462, 'duration': 2.421}, {'end': 890.188, 'text': "so let's run through the script one more time just to do kind of an overview of what what we're, what we've done here.", 'start': 883.883, 'duration': 6.305}, {'end': 892.209, 'text': 'uh, since this can get kind of confusing for people.', 'start': 890.188, 'duration': 2.021}, {'end': 897.373, 'text': 'so, um, importing threading, imported queue Q is its own little thing.', 'start': 892.209, 'duration': 5.164}], 'summary': 'Threading allows parallel work, reducing idle time. requires locks for coordination.', 'duration': 26.439, 'max_score': 870.934, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA870934.jpg'}, {'end': 1006.657, 'src': 'heatmap', 'start': 930.624, 'weight': 3, 'content': [{'end': 932.465, 'text': "So we've got print lock.", 'start': 930.624, 'duration': 1.841}, {'end': 932.865, 'text': "it's a lock.", 'start': 932.465, 'duration': 0.4}, {'end': 935.897, 'text': "Here, we've just defined an example job.", 'start': 933.735, 'duration': 2.162}, {'end': 942.443, 'text': "That job is just mimicking any sort of rough computing, saying it's taking a half a second to compute whatever it is.", 'start': 935.937, 'duration': 6.506}, {'end': 949.53, 'text': "And then we're saying with that lock we can print some information and when it's done printing, it releases the lock and then someone else can print.", 'start': 943.684, 'duration': 5.846}, {'end': 955.944, 'text': 'Then we have threader and basically the threader is literally a core function here in the core idea.', 'start': 950.68, 'duration': 5.264}, {'end': 958.566, 'text': 'And the idea here is that we can thread things.', 'start': 956.184, 'duration': 2.382}, {'end': 964.53, 'text': "So we're saying worker or the thread, right? We're saying q.get and we get the thread or worker.", 'start': 958.946, 'duration': 5.584}, {'end': 967.813, 'text': 'And then we run that worker through example job.', 'start': 964.55, 'duration': 3.263}, {'end': 971.835, 'text': 'And then as soon as that worker is done, we say queue task done.', 'start': 968.433, 'duration': 3.402}, {'end': 981.001, 'text': "And basically what we've done is we've released that thread or daemon back to the queue and say, okay, he's available again to be given some work.", 'start': 972.256, 'duration': 8.745}, {'end': 983.063, 'text': "So that's the threader.", 'start': 982.342, 'duration': 0.721}, {'end': 990.628, 'text': 'Now then we come down here and we actually start using these functions and we say, okay, queue equals queue for X in range of 10.', 'start': 983.123, 'duration': 7.505}, {'end': 991.729, 'text': 'So these are our 10 threads.', 'start': 990.628, 'duration': 1.101}, {'end': 994.41, 'text': "We're saying T equals threading a thread target equals threader.", 'start': 991.769, 'duration': 2.641}, {'end': 998.233, 'text': "So basically we're saying that T is kind of like our worker sort of or a thread.", 'start': 994.43, 'duration': 3.803}, {'end': 1006.657, 'text': 'its target is to run through threader, which will run them through example job again.', 'start': 999.293, 'duration': 7.364}], 'summary': 'Using print lock, threader, and example job to manage threads for computing and printing tasks.', 'duration': 42.107, 'max_score': 930.624, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA930624.jpg'}, {'end': 1115.188, 'src': 'embed', 'start': 1086.888, 'weight': 4, 'content': [{'end': 1088.948, 'text': 'And we saw that it was basically like 1.05 seconds.', 'start': 1086.888, 'duration': 2.06}, {'end': 1094.889, 'text': 'So anyway, hopefully that is a useful overview of the threading functionality.', 'start': 1089.048, 'duration': 5.841}, {'end': 1096.549, 'text': "Hopefully it's a little bit better.", 'start': 1095.469, 'duration': 1.08}, {'end': 1099.55, 'text': "I wasn't too happy with my original threading tutorial for Python 2.7.", 'start': 1096.589, 'duration': 2.961}, {'end': 1103.691, 'text': 'So kind of nice to be able to go through it again with Python 3,', 'start': 1099.55, 'duration': 4.141}, {'end': 1107.592, 'text': "since I have a slightly better understanding of how threading works now and I've actually used it.", 'start': 1103.691, 'duration': 3.901}, {'end': 1110.324, 'text': 'various jobs.', 'start': 1109.523, 'duration': 0.801}, {'end': 1115.188, 'text': 'so anyways, if you guys have any questions or comments, please feel free to leave those in the section below.', 'start': 1110.324, 'duration': 4.864}], 'summary': 'Overview of threading functionality: 1.05 seconds, python 3 improvement, various jobs.', 'duration': 28.3, 'max_score': 1086.888, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA1086888.jpg'}], 'start': 609.793, 'title': 'Implementing threading in python', 'summary': 'Discusses the implementation of threading in python, including the use of locks for synchronization, defining the target thread, processing 20 jobs with 10 threads, and highlighting python 3 advantages, resulting in a total execution time of around one second.', 'chapters': [{'end': 717.067, 'start': 609.793, 'title': 'Implementing threading in python', 'summary': 'Discusses the implementation of threading in python, including the use of locks for synchronization and the definition of the target thread, with a focus on heavy processing and the release of locks after completion.', 'duration': 107.274, 'highlights': ['The example job involves heavy processing with a time delay of 0.5 seconds, demonstrating the time-consuming nature of the task.', 'The utilization of a print lock for synchronization is highlighted, emphasizing the necessity to release the lock after the completion of the operation.', "The definition of the target thread, 'threader', is emphasized as the final step in the process of implementing threading in Python."]}, {'end': 942.443, 'start': 717.067, 'title': 'Threading and job processing', 'summary': 'Discusses the implementation of threading in python to process 20 jobs with 10 threads, where each job takes 0.5 seconds, resulting in a total execution time of around one second, showcasing the efficiency of parallel processing.', 'duration': 225.376, 'highlights': ['Processing 20 jobs with 10 threads in Python The script demonstrates the utilization of 10 threads to process 20 jobs, showcasing the efficiency of parallel processing.', 'Total execution time of around one second Despite 20 tasks taking 0.5 seconds each, the total execution time is approximately one second, highlighting the effectiveness of parallel processing.', 'Efficiency of parallel processing in reducing execution time The use of 10 threads to process 20 jobs results in a reduced execution time, emphasizing the efficiency of parallel processing.', 'Demonstration of threading in Python for job processing The chapter provides a demonstration of implementing threading in Python for processing jobs, showcasing its practical application.', 'Importance of locks in multithreading The importance of locks in multithreading is emphasized to avoid conflicts and ensure concurrent processing of different tasks.']}, {'end': 1121.973, 'start': 943.684, 'title': 'Threading in python', 'summary': 'Explains the concept of threading in python, demonstrating the use of threads to run 20 jobs in parallel, resulting in a total execution time of 1.05 seconds, and emphasizes the advantages of python 3 over python 2.7 for threading.', 'duration': 178.289, 'highlights': ['Demonstrates running 20 jobs in parallel using threading, achieving a total execution time of 1.05 seconds. The chapter showcases the use of threading to run 20 jobs in parallel, resulting in a total execution time of 1.05 seconds, illustrating the efficiency of threading for concurrent processing.', "Emphasizes the advantages of Python 3 over Python 2.7 for threading and the author's improved understanding and usage of threading. The author expresses a better understanding of threading in Python 3 compared to Python 2.7, highlighting the advantages of Python 3 and acknowledging personal growth in the practical usage of threading.", 'Explains the concept of a threader and its role in threading, including queuing tasks and releasing threads back to the queue after completion. The chapter provides an explanation of the threader, a core function in threading, detailing its role in queuing tasks, running workers through example jobs, and releasing completed threads back to the queue for further work.']}], 'duration': 512.18, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/NwH0HvMI4EA/pics/NwH0HvMI4EA609793.jpg', 'highlights': ['The chapter showcases the use of threading to run 20 jobs in parallel, resulting in a total execution time of 1.05 seconds, illustrating the efficiency of threading for concurrent processing.', 'The example job involves heavy processing with a time delay of 0.5 seconds, demonstrating the time-consuming nature of the task.', 'The utilization of a print lock for synchronization is highlighted, emphasizing the necessity to release the lock after the completion of the operation.', "The definition of the target thread, 'threader', is emphasized as the final step in the process of implementing threading in Python.", 'The author expresses a better understanding of threading in Python 3 compared to Python 2.7, highlighting the advantages of Python 3 and acknowledging personal growth in the practical usage of threading.', 'The script demonstrates the utilization of 10 threads to process 20 jobs, showcasing the efficiency of parallel processing.', 'Despite 20 tasks taking 0.5 seconds each, the total execution time is approximately one second, highlighting the effectiveness of parallel processing.', 'The use of 10 threads to process 20 jobs results in a reduced execution time, emphasizing the efficiency of parallel processing.', 'The chapter provides a demonstration of implementing threading in Python for processing jobs, showcasing its practical application.', 'The importance of locks in multithreading is emphasized to avoid conflicts and ensure concurrent processing of different tasks.', 'The chapter provides an explanation of the threader, a core function in threading, detailing its role in queuing tasks, running workers through example jobs, and releasing completed threads back to the queue for further work.']}], 'highlights': ['The tutorial covers python threading basics, emphasizing the importance of locks to prevent data corruption, and demonstrates multithreading with 10 worker threads performing tasks concurrently, resulting in a total execution time of around one second for 20 jobs.', 'The potential consequences of not using locks in threading, leading to data corruption and overlapping output, are highlighted with the example of a jumbled mess of text in a print function due to lack of synchronization.', 'The importance of using locks in threading to prevent data corruption when dealing with shared variables is emphasized, with the example of using a print lock in a threading tutorial.', 'The need to handle shared variables carefully in threading to avoid conflicting updates and ensure accurate results is underscored, demonstrating the importance of managing concurrent access to shared resources.', 'The concept of threading in Python is introduced, emphasizing the ability to perform multiple tasks simultaneously, utilizing the processing power more efficiently.', 'The potential issue of shared variables in threading is explained through an example involving the simultaneous modification of a variable by multiple threads, highlighting the incorrectness of the results due to concurrent updates.', 'The process involves assigning 20 instances for the workers and putting them to work in a queue, followed by waiting for the threads to terminate and printing the time taken for the entire job to complete.', 'The chapter discusses measuring the time taken for performance calculations, including organizing 20 jobs for 10 workers and analyzing the execution time of the process with Python threads.', 'The chapter provides a demonstration of implementing threading in Python for processing jobs, showcasing its practical application.', 'The chapter showcases the use of threading to run 20 jobs in parallel, resulting in a total execution time of 1.05 seconds, illustrating the efficiency of threading for concurrent processing.']}