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,510 points
1,350 views

2 answers to this question.

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
• 58,020 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, 2019 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, 2019 in Python by anonymous
1,069 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
• 58,020 points
1,058 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
• 13,300 points
6,230 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
• 58,020 points
5,152 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,078 views
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
• 58,020 points
686 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
• 58,020 points
1,139 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP