Questions tagged [python-multithreading]

python-multithreading refers to how to divide work into multiple streams of execution in Python.

python-multithreading refers to how to divide work into multiple streams of execution within a single process in Python. Usually this refers to the threading module. It could also refer to concurrent.futures.ThreadPoolExecutor or the thread/_thread module.

More information:

3995 questions
78
votes
3 answers

Pass keyword arguments to target function in Python threading.Thread

I want to pass named arguments to the target function, while creating a Thread object. Following is the code that I have written: import threading def f(x=None, y=None): print x,y t = threading.Thread(target=f, args=(x=1,y=2,)) t.start() I…
74
votes
12 answers

The right way to limit maximum number of threads running at once?

I'd like to create a program that runs multiple light threads, but limits itself to a constant, predefined number of concurrent running tasks, like this (but with no risk of race condition): import threading def f(arg): global running …
d33tah
  • 10,999
  • 13
  • 68
  • 158
72
votes
5 answers

A very simple multithreading parallel URL fetching (without queue)

I spent a whole day looking for the simplest possible multithreaded URL fetcher in Python, but most scripts I found are using queues or multiprocessing or complex libraries. Finally I wrote one myself, which I am reporting as an answer. Please feel…
Daniele B
  • 19,801
  • 29
  • 115
  • 173
71
votes
13 answers

Return value from thread

How do I get a thread to return a tuple or any value of my choice back to the parent in Python?
pjay
  • 2,422
  • 4
  • 21
  • 20
59
votes
4 answers

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

From what I understand, the GIL makes it impossible to have threads that harness a core each individually. This is a basic question, but, what is then the point of the threading library? It seems useless if the threaded code has equivalent speed to…
49
votes
6 answers

Semaphores on Python

I've started programming in Python a few weeks ago and was trying to use Semaphores to synchronize two simple threads, for learning purposes. Here is what I've got: import threading sem = threading.Semaphore() def fun1(): while True: …
48
votes
1 answer

Python Threading with Event object

I've seen a lot of Python scripts that use Threads in a class and a lot of them use the threading.Event(). For example: class TimerClass(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.event =…
user2724899
  • 483
  • 1
  • 4
  • 4
48
votes
3 answers

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

I am using the I/O non-blocking python server Tornado. I have a class of GET requests which may take a significant amount of time to complete (think in the range of 5-10 seconds). The problem is that Tornado blocks on these requests so that…
Rocketman
  • 3,464
  • 5
  • 27
  • 30
46
votes
4 answers

Multithreading for Python Django

Some functions should run asynchronously on the web server. Sending emails or data post-processing are typical use cases. What is the best (or most pythonic) way write a decorator function to run a function asynchronously? My setup is a common one:…
tomcounsell
  • 4,991
  • 3
  • 34
  • 34
45
votes
5 answers

Cancellable threading.Timer in Python

I am trying to write a method that counts down to a given time and unless a restart command is given, it will execute the task. But I don't think Python threading.Timer class allows for timer to be cancelable. import threading def…
Ted
  • 723
  • 2
  • 10
  • 19
40
votes
2 answers

Time-Limited Input?

What I would like to be able to do is ask a user a question using input. For example: print('some scenario') prompt = input("You have 10 seconds to choose the correct answer...\n") and then if the time elapses print something like print('Sorry,…
cloud311
  • 3,101
  • 5
  • 20
  • 20
38
votes
2 answers

How to pass arguments to a thread?

I have test() as shown below: def test(arg1, arg2=None, arg3=None): Now, I tries to create a thread using test(), and giving it only arg1 and arg2 but not arg3 as shown below: threading.Thread(target=test, args=(arg1, arg2=arg2)).start() But, I…
Dylan
  • 949
  • 3
  • 13
  • 23
37
votes
3 answers

numpy and Global Interpreter Lock

I am about to write some computationally-intensive Python code that'll almost certainly spend most of its time inside numpy's linear algebra functions. The problem at hand is embarrassingly parallel. Long story short, the easiest way for me to take…
NPE
  • 486,780
  • 108
  • 951
  • 1,012
37
votes
1 answer

TypeError in Threading. function takes x positional argument but y were given

I am working with Python at the moment. I have a start-function, that gets a string from a message. I want to start threads for every message. The thread at the moment should just print out my message like this: def startSuggestworker(message): …
TomHere
  • 469
  • 1
  • 7
  • 13
37
votes
3 answers

Python - appending to same file from multiple threads

I'm writing an app that appends lines to the same file from multiple threads. I have a problem in which some lines are appended without a new line. Any solution for this? class PathThread(threading.Thread): def __init__(self, queue): …
user1251654
  • 1,097
  • 4
  • 13
  • 21