How to use threading in Python?

0 votes

I am trying to understand threading in Python. I've looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I'm having trouble understanding them.

How do you clearly show tasks being divided for multi-threading

Jul 27, 2018 in Python by bug_seeker
• 15,300 points
53 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Here's a simple example: you need to try a few alternative URLs and return the contents of the first one to respond.

import Queue

import threading

import urllib2

# called by each thread

def get_url(q, url):

q.put(urllib2.urlopen(url).read())

theurls = ["http://google.com", "http://yahoo.com"]

q = Queue.Queue()

for u in theurls:

t = threading.Thread(target=get_url, args = (q,u))

t.daemon = True t.start()

s = q.get()

print s

This is a case where threading is used as a simple optimization: each subthread is waiting for a URL to resolve and respond, in order to put its contents on the queue; each thread is a daemon (won't keep the process up if main thread ends -- that's more common than not); the main thread starts all subthreads, does a get on the queue to wait until one of them has done a put, then emits the results and terminates (which takes down any subthreads that might still be running, since they're daemon threads).

Proper use of threads in Python is invariably connected to I/O operations (since CPython doesn't use multiple cores to run CPU-bound tasks anyway, the only reason for threading is not blocking the process while there's a wait for some I/O). Queues are almost invariably the best way to farm out work to threads and/or collect the work's results, by the way, and they're intrinsically threadsafe so they save you from worrying about locks, conditions, events, semaphores, and other inter-thread coordination/communication concepts.

answered Jul 27, 2018 by Priyaj
• 56,140 points
0 votes

 Thread is the smallest unit of processing that can be performed in an operating system. you can assume that a thread is simply a subset of a process.  thread is a sequence of such instructions within a program that can be executed independently of other code.

 In Python concept multi threading is defined as the ability of a processor to execute multiple threads concurrently. example of multi threading .

import thread
import time
# define functrion
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )
# create thread
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
except:
   print "Error: unable to start thread"
while 1:
   pass

output:

Thread-1: Sat Apr 06 11:42:19 20019
Thread-1: Sat Apr 06 11:42:21 20019
Thread-1: Sat Apr 06 11:42:23 20019

answered Apr 6 by anonymous

Related Questions In Python

+1 vote
2 answers

How to use the pass statement in Python

In Python programming, pass is a null statement. The ...READ MORE

answered Apr 5 in Python by anonymous
34 views
0 votes
1 answer

how to use Binary tree search in Python

The key thing to notice is that ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 56,140 points
48 views
0 votes
1 answer

How to use multiprocessing queue in Python?

This is a simple example of a ...READ MORE

answered Oct 25, 2018 in Python by SDeb
• 10,940 points
136 views
0 votes
1 answer

How to use HashMap in Python?

You can use Python dictionary it is a built-in type ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 56,140 points
571 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
58 views
+4 votes
6 answers
0 votes
1 answer

How to use “raise” keyword in Python

You can use it to raise errors ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 56,140 points
21 views
0 votes
1 answer

How to use string.replace() in python 3.x

replace() is a method of <class 'str'> ...READ MORE

answered Aug 3, 2018 in Python by Priyaj
• 56,140 points
29 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.