How to get the return value from a thread?

One way I’ve seen is to pass a mutable object, such as a list or a dictionary, to the thread’s constructor, along with a an index or other identifier of some sort. The thread can then store its results in its dedicated slot in that object. For example: def foo(bar, result, index): print ‘hello {0}’.format(bar) … Read more

Cancellable threading.Timer in Python

You would call the cancel method after you start the timer: import time import threading def hello(): print “hello, world” time.sleep(2) t = threading.Timer(3.0, hello) t.start() var=”something” if var == ‘something’: t.cancel() You might consider using a while-loop on a Thread, instead of using a Timer. Here is an example appropriated from Nikolaus Gradwohl’s answer … Read more

Multithreading for Python Django

I’ve continued using this implementation at scale and in production with no issues. Decorator definition: def start_new_thread(function): def decorator(*args, **kwargs): t = Thread(target = function, args=args, kwargs=kwargs) t.daemon = True t.start() return decorator Example usage: @start_new_thread def foo(): #do stuff Over time, the stack has updated and transitioned without fail. Originally Python 2.4.7, Django 1.4, … Read more

How to best perform Multiprocessing within requests with the python Tornado server?

If you’re willing to use concurrent.futures.ProcessPoolExecutor instead of multiprocessing, this is actually very simple. Tornado’s ioloop already supports concurrent.futures.Future, so they’ll play nicely together out of the box. concurrent.futures is included in Python 3.2+, and has been backported to Python 2.x. Here’s an example: import time from concurrent.futures import ProcessPoolExecutor from tornado.ioloop import IOLoop from … Read more

What’s the point of multithreading in Python if the GIL exists?

In some cases an application may not utilize even one core fully and using threads (or processes) may help to do that. Think of a typical web application. It receives requests from clients, does some queries to the database and returns data back to the client. Given that IO operation is order of magnitude slower … Read more

A very simple multithreading parallel URL fetching (without queue)

Simplifying your original version as far as possible: import threading import urllib2 import time start = time.time() urls = [“http://www.google.com”, “http://www.apple.com”, “http://www.microsoft.com”, “http://www.amazon.com”, “http://www.facebook.com”] def fetch_url(url): urlHandler = urllib2.urlopen(url) html = urlHandler.read() print “‘%s\’ fetched in %ss” % (url, (time.time() – start)) threads = [threading.Thread(target=fetch_url, args=(url,)) for url in urls] for thread in threads: thread.start() … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)