So how do you "talk" to a thread? Some frameworks, like Qt, have signal/slot mechanisms for passing info back and forth. Or if you have some asynchronous code that needs to wait for inputs from different sources. Especially in GUI programming, you will want to offload as much as possible to side threads. For example, what if you want to get user input while also executing some complicated tasks? You don't want to freeze the heavy background computation while waiting for input, and you don't want the user input to feel stuck because the background process is taking a long time. But I want to instead get into a more complicated case, having two long lived threads that need to talk to each other. The previous code is great for simple functions, and there are more complicated things like thread pools that you can play with. That way, you make sure that func1 finishes before func3 starts. When called, it blocks execution of that thread until the other thread is finished. But what if you want a and b to be modified? That is where the join() method comes in. If a and b are meant to be constants that's fine (here is where some of python's most glaring flaws turn up, the lack of constants and the passing of all arguments by object reference). If you put all three into threads, you have no idea when what is called. Assuming these are modified by func1, if you just call func3 right after func2 is done it's possible that func1 will still be running. Notice that func3 also takes variables a and b. You then call func2, and that's it! You now have two threads running. The start method calls the function, and begins execution. To create a thread, you define a function you want executed by a thread and set it to target, and then pass in some arguments. The Thread object is imported from the threading module, part of the standard library ( docs). For example, lets say my code looks like the following:Įnter fullscreen mode Exit fullscreen mode The challenge in creating threads is that now you no longer know the execution order of your program. Side note: You can have more than one python process execute using the multiprocessing module, but that's beyond the scope of this post. But what if there is a waiting step in your code? Now you have a golden opportunity to make your computer work while you wait. In other words, if you just want speed there might be ways to more efficiently organize your code and get the same speed boost that multithreading would give you. This is where threads can speed up your code. However, sometimes your program might have a waiting step, and you could be executing some other code in the background. But in python, you can't actually execute two threads at the same time, because of something called the Global Interpreter Lock. Modern CPUs are frequently multi-core architectures capable of executing code in parallel, and you may see this referred to as a PC executing multiple "threads". One very important thing to keep in mind is what a thread actually is in the context of python. Fortunately, python makes it very easy to get started with multithreading. Everyone at some point has a very dangerous thought: What if I used multithreading to speed up my app? This is dangerous because it now requires thinking about your code in a whole new way.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |