1

I need to know if there is a way I can just use multithreading in a single line, rather than the full program.

That is, I want to run the program as a single thread, but in the middle of it I want one particular line to be multithreaded. I want to multithread something like this:

br.open("http://www.google.com.pk/search?q="+str(m))

So, basically I want to open all the links (m can assume 50 distinct values), and I don't want to open them one after the other. I want to open all 50 at once! If I open them one by one, it slows the process down and I want to avoid that.

jamylak
  • 128,818
  • 30
  • 231
  • 230
Hoyo
  • 1,044
  • 3
  • 13
  • 23
  • 5
    Just as a general comment, unrelated to _how_ to do this: Each of those `open` calls sends an HTTP request, and you should be careful about how many requests you send to the server (especially, Google) at once. They may block you if you do a lot of this without proper breaks / sleeps in between. – jogojapan Jun 19 '12 at 07:42
  • 4
    Also, opening 50 threads at once doesn't necessarily make things faster. Anyway, here's some inspiration: [link](http://www.ibm.com/developerworks/aix/library/au-threadingpython/) – phipsgabler Jun 19 '12 at 07:44
  • Are you running the new 25-core hyperthreading CPU? – jedwards Jun 19 '12 at 07:49
  • @ jedwards I have Core i7 2600k - @ phg As for the threading it will open 50 links once not like sequence wise - and that sequence wise makes it slower because when I open it once it takes 2 -4 sec to load the page - – Hoyo Jun 19 '12 at 07:58

3 Answers3

3

You want to look at the threading.Thread class.

import threading

def worker():
    """thread worker function"""
    print 'Worker'
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()

If you are doing multiple HTTP requests you may also want to look at queue.Queue You would then queue up multiple http requests. Here is a nice example of that in action.

msanders
  • 497
  • 2
  • 8
1

You are after asynchronous communications. I haven't tried it myself, but take a look at grequests. It used to be part of the requests library, but has been factored out. Usage (from their github page) seems very easy:

import grequests

urls = [
    'http://www.heroku.com',
    'http://tablib.org',
    'http://httpbin.org',
    'http://python-requests.org',
    'http://kennethreitz.com'
]
rs = (grequests.get(u) for u in urls)
grequests.map(rs)

If you want to keep within the Python standard library, you might take a look at the asyncore module.

Also, another alternative is Twisted. It might be overkill for your requirements, and the learning curve is notoriously steep.

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

I would here be actually trying to answer the original question asked by the OP, I don't recommend this code due to obvious readability issues but just like in Java you can actually run a thread from a single line in Python.

We will be using the Thread Class from Python

from threading import Thread

Define the Function which you want to call.

def func1():
   print("Hello World")

Start a thread from one single line

Thread(target=func1).start()

That is how you start one liner thread in python.
For information on how to pass arguments to target function

Rohan Sawant
  • 938
  • 1
  • 11
  • 18