🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 73 (from laksa191)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
CRAWLED
6 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.2 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://devarea.com/python-multitasking-multithreading-multiprocessing/
Last Crawled2026-04-11 08:31:45 (6 days ago)
First Indexed2019-04-04 17:26:08 (7 years ago)
HTTP Status Code200
Meta TitlePython Multitasking – MultiThreading and MultiProcessing – Developers Area
Meta Descriptionnull
Meta Canonicalnull
Boilerpipe Text
Modern operating systems allow a user to run several applications simultaneously, so that some tasks can be run in the background while the user continues with other work in the foreground. The user can also run multiple copies of the same program at the same time. To create a task we can use process or thread. Process has its private resources including memory mapping, files and other os objects. Multiple threads can run on the same process and share all its resources but if one thread fail it will kill all other threads in its process. Python has many packages to handle multi tasking, in this post i will cover some os.fork Unix/Linux/OS X specific (i.e. all but windows). The function creates a child process that start running after the fork return. We call fork once but it returns twice on the parent and on the child. Consider the following code: import os x=os.fork() if x: print('hello parent') else: print('hello child') If you run it , you will see both prints: hello parent hello child On fork call, the OS creates a child process and fork returns twice – on the parent the return value is the child process id and on the child fork returns 0. The point behind this weird behaviour is the Join-Fork parallel pattern – you run a code and want to use another processor for a complex task – fork a child, divide the work and join again: import os ret=0 # we need to do something complex so lets create a child x=os.fork() if x: print('do half work by parent') ret = os.wait() else: print('do half work by child') os._exit(10) print("only parent here res=" + str(os.WEXITSTATUS(ret[1]))) Before the fork call and after the if statement only one process exists , The child does its task and finish returning a value to the parent, If the parent finish before the child it will wait for him It is important to call wait otherwise the child remains zombie os.system Runs a command in the shell (bash, cmd, etc) sometimes you only need to run another program or command for example you want to do something that easily done with a command like format import os status = os.system('script2.py') print ("exit status",status) os.popen Run a process and read its output as a file for example: import os for line in os.popen('ps aux').readlines(): print(line) The subprocess package An attempt to replace os specific function with platform independent. You can run a process and open a pipe in any os for example: import subprocess subprocess.run('script2.py') pr = subprocess.Popen('ls') pr.wait(); to communicate with the output use: import subprocess import sys proc = subprocess.Popen([sys.executable, 'myapp.py', 'data'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) (output, error) = proc.communicate() if error != None: print("error:", error.decode()) print("output:", output.decode()) The multiprocessing package Another useful package for process creation and other methods. The multiprocessing module is suitable for sharing data or tasks between processor cores. It does not use threading, but processes instead. Processes are inherently more “expensive” that threads, so they are not worth using for trivial data sets or tasks import multiprocessing def fn(a,b): print ("vals=",a,b) proc1 = multiprocessing.Process(target=fn, args=(10,20)) proc2 = multiprocessing.Process(target=fn, args=(10,20)) proc1.start() proc2.start() proc1.join() proc2.join() Working with Threads Threads run on the same process address space – it is easy to share data between them but if one thread fails all other threads in the same process killed. To create a thread we use the threading package. We write a class derived from Thread and declare the function run. To create a thread we need to create an object from the class and call start method: import threading import time class MyThread (threading.Thread): def run (self): for i in range(5): print ("In thread", self.name) time.sleep(2) th1 = MyThread() th2 = MyThread() th1.setName("t1") th2.setName("t2") th1.start() th2.start() print ("From main") th1.join() th2.join() output: In thread t1 In thread t2 From main In thread t1 In thread t2 In thread t1 In thread t2 .... You can also define the thread function as global (without class): from threading import Thread import time def myfunc(*args): print "From thread", args time.sleep(5) tid1 = Thread(target=myfunc, args='T1') tid2 = Thread(target=myfunc, args='T2') tid1.start() tid2.start() print "From main" tid1.join() tid2.join() Synchronization Objects Accessing resources needs synchronization. We can find the following objects in threading package: Condition variables – similar to linux posix condition variables Events – similar to windows events Lock – similar to mutex Semaphore Lock Example: import threading import time lc = threading.Lock() def myfunc1(*args): global lc lc.acquire() print("From thread", args) lc.release() def myfunc2(*args): global lc lc.acquire() print("From thread", args) lc.release() tid1 = threading.Thread(target=myfunc1, args='T1') tid2 = threading.Thread(target=myfunc2, args='T2') tid1.start() tid2.start() print("From main") tid1.join() tid2.join() Inter Process Communication (IPC) Threads share the same address space so its easy to send data from one thread to another but processes live in a different address spaces and thats why we need an IPC object. You can use pipe, fifo, message queue and more Message Queue Example: In the following example we create 2 processes and use a queue for communication. One process send message to the queue and the other receive: import multiprocessing import time def fn1(q): while True: x=q.get() print ("val=",x) def fn2(q): while True: time.sleep(2) q.put("hello") queue = multiprocessing.Queue() proc1 = multiprocessing.Process(target=fn1, args=(queue,)) proc2 = multiprocessing.Process(target=fn2, args=(queue,)) proc1.start() proc2.start() You can find more methods for creating threads and processes, using synchronization objects and IPC
Markdown
[Developers Area](https://devarea.com/ "Developers Area") [Just another Developers site](https://devarea.com/ "Just another Developers site") Toggle navigation - [Home](https://devarea.com/) - [Labs](https://devarea.com/labs/) - [Nice Projects links](https://devarea.com/nice-projects-links/) - [GitHub](https://github.com/dev-area) - [Contact Me](https://devarea.com/contact/) ## [Python Multitasking – MultiThreading and MultiProcessing](https://devarea.com/python-multitasking-multithreading-multiprocessing/) By [Liran B.H](https://devarea.com/author/liran/) \| [November 30, 2017](https://devarea.com/2017/11/30/) \| [3 Comments](https://devarea.com/python-multitasking-multithreading-multiprocessing/#comments) \| [python](https://devarea.com/category/python/ "View all posts in python") Modern operating systems allow a user to run several applications simultaneously, so that some tasks can be run in the background while the user continues with other work in the foreground. The user can also run multiple copies of the same program at the same time. To create a task we can use process or thread. Process has its private resources including memory mapping, files and other os objects. Multiple threads can run on the same process and share all its resources but if one thread fail it will kill all other threads in its process. Python has many packages to handle multi tasking, in this post i will cover some ### os.fork Unix/Linux/OS X specific (i.e. all but windows). The function creates a child process that start running after the fork return. We call fork once but it returns twice on the parent and on the child. Consider the following code: ``` import os x=os.fork() if x: print('hello parent') else: print('hello child') ``` If you run it , you will see both prints: ``` hello parent hello child ``` On fork call, the OS creates a child process and fork returns twice – on the parent the return value is the child process id and on the child fork returns 0. The point behind this weird behaviour is the Join-Fork parallel pattern – you run a code and want to use another processor for a complex task – fork a child, divide the work and join again: ``` import os ret=0 # we need to do something complex so lets create a child x=os.fork() if x: print('do half work by parent') ret = os.wait() else: print('do half work by child') os._exit(10) print("only parent here res=" + str(os.WEXITSTATUS(ret[1]))) ``` Before the fork call and after the if statement only one process exists , The child does its task and finish returning a value to the parent, If the parent finish before the child it will wait for him It is important to call wait otherwise the child remains zombie ### os.system Runs a command in the shell (bash, cmd, etc) sometimes you only need to run another program or command for example you want to do something that easily done with a command like format ``` import os status = os.system('script2.py') print ("exit status",status) ``` ### os.popen Run a process and read its output as a file for example: ``` import os for line in os.popen('ps aux').readlines(): print(line) ``` ### The subprocess package An attempt to replace os specific function with platform independent. You can run a process and open a pipe in any os for example: ``` import subprocess subprocess.run('script2.py') pr = subprocess.Popen('ls') pr.wait(); ``` to communicate with the output use: ``` import subprocess import sys proc = subprocess.Popen([sys.executable, 'myapp.py', 'data'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) (output, error) = proc.communicate() if error != None: print("error:", error.decode()) print("output:", output.decode()) ``` ### The multiprocessing package Another useful package for process creation and other methods. The multiprocessing module is suitable for sharing data or tasks between processor cores. It does not use threading, but processes instead. Processes are inherently more “expensive” that threads, so they are not worth using for trivial data sets or tasks ``` import multiprocessing def fn(a,b): print ("vals=",a,b) proc1 = multiprocessing.Process(target=fn, args=(10,20)) proc2 = multiprocessing.Process(target=fn, args=(10,20)) proc1.start() proc2.start() proc1.join() proc2.join() ``` ### Working with Threads Threads run on the same process address space – it is easy to share data between them but if one thread fails all other threads in the same process killed. To create a thread we use the **threading** package. We write a class derived from Thread and declare the function run. To create a thread we need to create an object from the class and call start method: ``` import threading import time class MyThread (threading.Thread): def run (self): for i in range(5): print ("In thread", self.name) time.sleep(2) th1 = MyThread() th2 = MyThread() th1.setName("t1") th2.setName("t2") th1.start() th2.start() print ("From main") th1.join() th2.join() ``` output: ``` In thread t1 In thread t2 From main In thread t1 In thread t2 In thread t1 In thread t2 .... ``` You can also define the thread function as global (without class): ``` from threading import Thread import time def myfunc(*args): print "From thread", args time.sleep(5) tid1 = Thread(target=myfunc, args='T1') tid2 = Thread(target=myfunc, args='T2') tid1.start() tid2.start() print "From main" tid1.join() tid2.join() ``` ### ### Synchronization Objects Accessing resources needs synchronization. We can find the following objects in threading package: - Condition variables – similar to linux posix condition variables - Events – similar to windows events - Lock – similar to mutex - Semaphore #### Lock Example: ``` import threading import time lc = threading.Lock() def myfunc1(*args): global lc lc.acquire() print("From thread", args) lc.release() def myfunc2(*args): global lc lc.acquire() print("From thread", args) lc.release() tid1 = threading.Thread(target=myfunc1, args='T1') tid2 = threading.Thread(target=myfunc2, args='T2') tid1.start() tid2.start() print("From main") tid1.join() tid2.join() ``` ### ### Inter Process Communication (IPC) Threads share the same address space so its easy to send data from one thread to another but processes live in a different address spaces and thats why we need an IPC object. You can use pipe, fifo, message queue and more #### Message Queue Example: In the following example we create 2 processes and use a queue for communication. One process send message to the queue and the other receive: ``` import multiprocessing import time def fn1(q): while True: x=q.get() print ("val=",x) def fn2(q): while True: time.sleep(2) q.put("hello") queue = multiprocessing.Queue() proc1 = multiprocessing.Process(target=fn1, args=(queue,)) proc2 = multiprocessing.Process(target=fn2, args=(queue,)) proc1.start() proc2.start() ``` You can find more methods for creating threads and processes, using synchronization objects and IPC Tagged [python](https://devarea.com/tag/python/) ## Post navigation [← Python Machine Learning Example – Linear Regression](https://devarea.com/python-machine-learning-example-linear-regression/) [Linux – Handling Signals in a Multithreaded Application →](https://devarea.com/linux-handling-signals-in-a-multithreaded-application/) ## 3 thoughts on “Python Multitasking – MultiThreading and MultiProcessing” 1. ![](https://secure.gravatar.com/avatar/c941b0ff8558631eafac4df0d5f21514a3a74cdc4973a8646ad99f4808273a2f?s=128&d=mm&r=g) [anushri](https://www.besanttechnologies.com/training-courses/python-training-institute-in-bangalore) Multiple threading are useful create program small size its use full to workout. [March 1, 2018](https://devarea.com/python-multitasking-multithreading-multiprocessing/#comment-602) \- 2. ![](https://secure.gravatar.com/avatar/f3c8a19e7523417bf916d633e3f1abde7d123c580d0b9d238fbc4e903c474b22?s=128&d=mm&r=g) Sergio Many thanks, very useful post\! [May 28, 2019](https://devarea.com/python-multitasking-multithreading-multiprocessing/#comment-7016) \- 3. ![](https://secure.gravatar.com/avatar/fde9409b241151326a7ce105e5cb1bc44ad4f1a9a948512f912312ed7fe8f0f4?s=128&d=mm&r=g) [mama bear t shirt](http://ow.ly/pqtI50wN35r) Hi friends, its fantastic post on the topic of teachingand fully defined, keep it up all the time. [November 30, 2019](https://devarea.com/python-multitasking-multithreading-multiprocessing/#comment-8856) \- Comments are closed. ### Subscribe to our Newsletter ### Categories - [Android](https://devarea.com/category/android/) - [Angular](https://devarea.com/category/angular/) - [Django](https://devarea.com/category/django/) - [General](https://devarea.com/category/general/) - [Linux](https://devarea.com/category/linux/) - [Machine Learning](https://devarea.com/category/machine-learning/) - [python](https://devarea.com/category/python/) ### Recent Posts - [Building a Web App with Angular, Django and Django REST](https://devarea.com/building-a-web-app-with-angular-django-and-django-rest/) - [Introduction To Network Filters – Linux](https://devarea.com/introduction-to-network-filters-linux/) - [Linear Regression With Numpy](https://devarea.com/linear-regression-with-numpy/) - [Python – Working With Virtual Environments](https://devarea.com/python-working-with-virtual-environments/) - [Python – Text Processing Introduction](https://devarea.com/python-text-processing-introduction/) ### Tags [Android](https://devarea.com/tag/android/) [Android Internals](https://devarea.com/tag/android-internals/) [angular](https://devarea.com/tag/angular/) [AOSP](https://devarea.com/tag/aosp/) [Assembly](https://devarea.com/tag/assembly/) [C](https://devarea.com/tag/c/) [Debug](https://devarea.com/tag/debug/) [django](https://devarea.com/tag/django/) [git](https://devarea.com/tag/git/) [Java](https://devarea.com/tag/java/) [Kernel](https://devarea.com/tag/kernel/) [Linux](https://devarea.com/tag/linux/) [Machine Learning](https://devarea.com/tag/machine-learning/) [Networking](https://devarea.com/tag/networking/) [numpy](https://devarea.com/tag/numpy/) [pandas](https://devarea.com/tag/pandas/) [python](https://devarea.com/tag/python/) [rest](https://devarea.com/tag/rest/) [scipy](https://devarea.com/tag/scipy/) [security](https://devarea.com/tag/security/) ### Subscribe to our Newsletter ### Archives - [April 2019](https://devarea.com/2019/04/) - [March 2019](https://devarea.com/2019/03/) - [May 2018](https://devarea.com/2018/05/) - [March 2018](https://devarea.com/2018/03/) - [February 2018](https://devarea.com/2018/02/) - [January 2018](https://devarea.com/2018/01/) - [December 2017](https://devarea.com/2017/12/) - [November 2017](https://devarea.com/2017/11/) - [October 2017](https://devarea.com/2017/10/) - [September 2017](https://devarea.com/2017/09/) - [August 2017](https://devarea.com/2017/08/) - [May 2017](https://devarea.com/2017/05/) - [March 2017](https://devarea.com/2017/03/) ### Categories - [Android](https://devarea.com/category/android/) - [Angular](https://devarea.com/category/angular/) - [Django](https://devarea.com/category/django/) - [General](https://devarea.com/category/general/) - [Linux](https://devarea.com/category/linux/) - [Machine Learning](https://devarea.com/category/machine-learning/) - [python](https://devarea.com/category/python/) ### Recent Posts - [Building a Web App with Angular, Django and Django REST](https://devarea.com/building-a-web-app-with-angular-django-and-django-rest/) - [Introduction To Network Filters – Linux](https://devarea.com/introduction-to-network-filters-linux/) - [Linear Regression With Numpy](https://devarea.com/linear-regression-with-numpy/) - [Python – Working With Virtual Environments](https://devarea.com/python-working-with-virtual-environments/) - [Python – Text Processing Introduction](https://devarea.com/python-text-processing-introduction/) ### Tags [Android](https://devarea.com/tag/android/) [Android Internals](https://devarea.com/tag/android-internals/) [angular](https://devarea.com/tag/angular/) [AOSP](https://devarea.com/tag/aosp/) [Assembly](https://devarea.com/tag/assembly/) [C](https://devarea.com/tag/c/) [Debug](https://devarea.com/tag/debug/) [django](https://devarea.com/tag/django/) [git](https://devarea.com/tag/git/) [Java](https://devarea.com/tag/java/) [Kernel](https://devarea.com/tag/kernel/) [Linux](https://devarea.com/tag/linux/) [Machine Learning](https://devarea.com/tag/machine-learning/) [Networking](https://devarea.com/tag/networking/) [numpy](https://devarea.com/tag/numpy/) [pandas](https://devarea.com/tag/pandas/) [python](https://devarea.com/tag/python/) [rest](https://devarea.com/tag/rest/) [scipy](https://devarea.com/tag/scipy/) [security](https://devarea.com/tag/security/) ### Subscribe to our mailing list ### Follow Me *** [ShopIsle](http://themeisle.com/themes/shop-isle/) powered by [WordPress](http://wordpress.org/)
Readable Markdown
Modern operating systems allow a user to run several applications simultaneously, so that some tasks can be run in the background while the user continues with other work in the foreground. The user can also run multiple copies of the same program at the same time. To create a task we can use process or thread. Process has its private resources including memory mapping, files and other os objects. Multiple threads can run on the same process and share all its resources but if one thread fail it will kill all other threads in its process. Python has many packages to handle multi tasking, in this post i will cover some ### os.fork Unix/Linux/OS X specific (i.e. all but windows). The function creates a child process that start running after the fork return. We call fork once but it returns twice on the parent and on the child. Consider the following code: ``` import os x=os.fork() if x: print('hello parent') else: print('hello child') ``` If you run it , you will see both prints: ``` hello parent hello child ``` On fork call, the OS creates a child process and fork returns twice – on the parent the return value is the child process id and on the child fork returns 0. The point behind this weird behaviour is the Join-Fork parallel pattern – you run a code and want to use another processor for a complex task – fork a child, divide the work and join again: ``` import os ret=0 # we need to do something complex so lets create a child x=os.fork() if x: print('do half work by parent') ret = os.wait() else: print('do half work by child') os._exit(10) print("only parent here res=" + str(os.WEXITSTATUS(ret[1]))) ``` Before the fork call and after the if statement only one process exists , The child does its task and finish returning a value to the parent, If the parent finish before the child it will wait for him It is important to call wait otherwise the child remains zombie ### os.system Runs a command in the shell (bash, cmd, etc) sometimes you only need to run another program or command for example you want to do something that easily done with a command like format ``` import os status = os.system('script2.py') print ("exit status",status) ``` ### os.popen Run a process and read its output as a file for example: ``` import os for line in os.popen('ps aux').readlines(): print(line) ``` ### The subprocess package An attempt to replace os specific function with platform independent. You can run a process and open a pipe in any os for example: ``` import subprocess subprocess.run('script2.py') pr = subprocess.Popen('ls') pr.wait(); ``` to communicate with the output use: ``` import subprocess import sys proc = subprocess.Popen([sys.executable, 'myapp.py', 'data'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) (output, error) = proc.communicate() if error != None: print("error:", error.decode()) print("output:", output.decode()) ``` ### The multiprocessing package Another useful package for process creation and other methods. The multiprocessing module is suitable for sharing data or tasks between processor cores. It does not use threading, but processes instead. Processes are inherently more “expensive” that threads, so they are not worth using for trivial data sets or tasks ``` import multiprocessing def fn(a,b): print ("vals=",a,b) proc1 = multiprocessing.Process(target=fn, args=(10,20)) proc2 = multiprocessing.Process(target=fn, args=(10,20)) proc1.start() proc2.start() proc1.join() proc2.join() ``` ### Working with Threads Threads run on the same process address space – it is easy to share data between them but if one thread fails all other threads in the same process killed. To create a thread we use the **threading** package. We write a class derived from Thread and declare the function run. To create a thread we need to create an object from the class and call start method: ``` import threading import time class MyThread (threading.Thread): def run (self): for i in range(5): print ("In thread", self.name) time.sleep(2) th1 = MyThread() th2 = MyThread() th1.setName("t1") th2.setName("t2") th1.start() th2.start() print ("From main") th1.join() th2.join() ``` output: ``` In thread t1 In thread t2 From main In thread t1 In thread t2 In thread t1 In thread t2 .... ``` You can also define the thread function as global (without class): ``` from threading import Thread import time def myfunc(*args): print "From thread", args time.sleep(5) tid1 = Thread(target=myfunc, args='T1') tid2 = Thread(target=myfunc, args='T2') tid1.start() tid2.start() print "From main" tid1.join() tid2.join() ``` ### Synchronization Objects Accessing resources needs synchronization. We can find the following objects in threading package: - Condition variables – similar to linux posix condition variables - Events – similar to windows events - Lock – similar to mutex - Semaphore #### Lock Example: ``` import threading import time lc = threading.Lock() def myfunc1(*args): global lc lc.acquire() print("From thread", args) lc.release() def myfunc2(*args): global lc lc.acquire() print("From thread", args) lc.release() tid1 = threading.Thread(target=myfunc1, args='T1') tid2 = threading.Thread(target=myfunc2, args='T2') tid1.start() tid2.start() print("From main") tid1.join() tid2.join() ``` ### Inter Process Communication (IPC) Threads share the same address space so its easy to send data from one thread to another but processes live in a different address spaces and thats why we need an IPC object. You can use pipe, fifo, message queue and more #### Message Queue Example: In the following example we create 2 processes and use a queue for communication. One process send message to the queue and the other receive: ``` import multiprocessing import time def fn1(q): while True: x=q.get() print ("val=",x) def fn2(q): while True: time.sleep(2) q.put("hello") queue = multiprocessing.Queue() proc1 = multiprocessing.Process(target=fn1, args=(queue,)) proc2 = multiprocessing.Process(target=fn2, args=(queue,)) proc1.start() proc2.start() ``` You can find more methods for creating threads and processes, using synchronization objects and IPC
Shard73 (laksa)
Root Hash2261861757304640073
Unparsed URLcom,devarea!/python-multitasking-multithreading-multiprocessing/ s443