![]() |
|
[Python] [TUTORIAL]Basic Multithreading[TUTORIAL] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] [TUTORIAL]Basic Multithreading[TUTORIAL] (/showthread.php?tid=2925646) Pages: Pages
1
2
|
[TUTORIAL]Basic Multithreading[TUTORIAL] - yungdsl - Oct 7, 2012 Basic Multithreading Tutorial For this tutorial we will be using Python 2.7.3 Hello python section, I am back with another tutorial. Today, we are going to learn some basic multithreading. What is multithreading? Multithreading is more or less having two parts of your program execute at the same time, while still sharing the same data. For example, you could have two while loops going at the same time. Now, on to programming. First we will need to import the module for threading. Be sure to include time, we will be using it later. Code import threading, timeNow, we are going to have to have a function for the thread to use. Later on we can also build classes to use, but lets just do this for now. Lets make our function like this: Code import threading, timeThis is just a simple function, that will wait half of a second and then print the phrase Hello from worker (id). Now we will set up the threading like this: Code import threading, timeNow, when you run that you should get output something like this: [img]http://img692.imageshack.us/img692/7752/14c20dc026114f9e8ea85ce.png[/img] What this does so far, is makes two threads which use the function workerFunc. The id argument is passed to them. Then, the program starts thread01 and then waits 3.7 seconds, and then starts thread02. Thats the end of this tutorial! Part 2 coming soon with object orientation and more advanced design. * - D3vildog - Oct 7, 2012 Daym Son, that is some sexy multi-threading, pretty soon we will have Crysis 3 in Python. * - .php - Oct 7, 2012 Awesome :D Helped me gain a better understanding! * - yungdsl - Oct 7, 2012 (Oct 7, 2012 07:52 PM).php Wrote: Awesome :D Helped me gain a better understanding! Your welcome. Be sure to look at my next tutorial on threading, whenever I make it. Probably tonight. * - Integral - Oct 7, 2012 simple tutorial, but gets the point across very nicely - nice job. * - tulamba - Oct 7, 2012 Good job my friend. I have a question dude they are always repeating the pattern 2,1,2,1 .... if i have multi-core machine, which is the case with today's CPUs, then i can run them concurrently. It means CPU1 running thread1 and CPU2 running thread2. What are your thoughts on this? * - Asp1r3-At0m - Oct 7, 2012 you are a genius, i was trying to understand what the hell was threading and how do do it. thanks ! * - yungdsl - Oct 7, 2012 (Oct 7, 2012 08:29 PM)tulamba Wrote: Good job my friend.If you are wondering why they follow that pattern, its because thread 2 starts 3.7 seconds after thread1. And this should work the same on pcs with any amount of cores. (Oct 7, 2012 09:16 PM)Asp1r3-At0m Wrote: you are a genius, i was trying to understand what the hell was threading and how do do it. thanks ! Yes, I had the same trouble with it at first but the threading module was the easiest way to do it and it really helped me. * - Curator - Oct 7, 2012 (Oct 7, 2012 08:29 PM)tulamba Wrote: Good job my friend.Python's default implementation (called CPython) does not support concurrent multithreading on multicore systems. However, you can use the multiprocessing module and similar library calls to achieve true concurrency. Each worker is its own process instead of its own thread. * - FaggotFlounder - Oct 8, 2012 Informative. Thanks for sharing it with us :) * - Jaket25 - Oct 8, 2012 Great tutorial, little examples like this really do help explain new modules to me. Just trying to think of a practical use to implement this module into now! Haha Thanks a lot, we need more tutorials like this 🙂 * - tulamba - Oct 8, 2012 Thanks Curator and Kaonashi for your comments (Oct 7, 2012 11:51 PM)Curator Wrote: Python's default implementation (called CPython) does not support concurrent multithreading on multicore systems. * - dberube - Oct 8, 2012 The multiprocessing module works the same way: Code from multiprocessing import Process* - zapfen - Oct 23, 2012 Thats a nice tutorial :) Thank you! * - yungdsl - Oct 23, 2012 (Oct 23, 2012 12:41 PM)zapfen Wrote: Thats a nice tutorial :) Thank you! No problem, I'm going to do a tutorial on building classes for multithreading tonight. |