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
1485
votes
24 answers

How do I use threading in Python?

I would like a clear example showing tasks being divided across multiple threads.
albruno
  • 14,955
  • 3
  • 18
  • 6
977
votes
31 answers

Is there any way to kill a Thread?

Is it possible to terminate a running thread without setting/checking any flags/semaphores/etc.?
Sudden Def
  • 10,031
  • 3
  • 18
  • 8
615
votes
27 answers

How to get the return value from a thread?

The function foo below returns a string 'foo'. How can I get the value 'foo' which is returned from the thread's target? from threading import Thread def foo(bar): print('hello {}'.format(bar)) return 'foo' thread = Thread(target=foo,…
wim
  • 338,267
  • 99
  • 616
  • 750
500
votes
24 answers

Timeout on a function call

I'm calling a function in Python which I know may stall and force me to restart the script. How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script cancels it and does something else?
Teifion
  • 108,121
  • 75
  • 161
  • 195
295
votes
9 answers

Daemon Threads Explanation

In the Python documentation it says: A thread can be flagged as a "daemon thread". The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating…
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
291
votes
12 answers

What is the use of join() in threading?

I was studying the python threading and came across join(). The author told that if thread is in daemon mode then i need to use join() so that thread can finish itself before main thread terminates. but I have also seen him using t.join() even…
user192362127
  • 11,385
  • 8
  • 24
  • 21
268
votes
8 answers

How to obtain a Thread id in Python?

I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. I would…
260
votes
8 answers

Queue.Queue vs. collections.deque

I need a queue which multiple threads can put stuff into, and multiple threads may read from. Python has at least two queue classes, Queue.Queue and collections.deque, with the former seemingly using the latter internally. Both claim to be…
miracle2k
  • 29,597
  • 21
  • 65
  • 64
215
votes
4 answers

Are lists thread-safe?

I notice that it is often suggested to use queues with multiple threads, instead of lists and .pop(). Is this because lists are not thread-safe, or for some other reason?
lemiant
  • 4,205
  • 4
  • 31
  • 38
124
votes
16 answers

threading.Timer - repeat function every 'n' seconds

I want to fire off a function every 0.5 seconds and be able to start and stop and reset the timer. I'm not too knowledgeable of how Python threads work and am having difficulties with the python timer. However, I keep getting RuntimeError: threads…
user1431282
  • 6,535
  • 13
  • 51
  • 68
112
votes
4 answers

How does ThreadPoolExecutor().map differ from ThreadPoolExecutor().submit?

I was just very confused by some code that I wrote. I was surprised to discover that: with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: results = list(executor.map(f, iterable)) and with…
100
votes
4 answers

How to Multi-thread an Operation Within a Loop in Python

Say I have a very large list and I'm performing an operation like so: for item in items: try: api.my_operation(item) except: print 'error with item' My issue is two fold: There are a lot of items api.my_operation takes…
doremi
  • 14,921
  • 30
  • 93
  • 148
99
votes
6 answers

How to terminate a thread when main program ends?

If I have a thread in an infinite loop, is there a way to terminate it when the main program ends (for example, when I press Ctrl+C)?
facha
  • 11,862
  • 14
  • 59
  • 82
87
votes
5 answers

Thread vs. Threading

What's the difference between the threading and thread modules in Python?
banx
  • 4,376
  • 4
  • 30
  • 34
81
votes
3 answers

Meaning of daemon property on Python Threads

I'm a little confused about what setting a thread to be a daemon means. The documentation says this: A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads…
Falmarri
  • 47,727
  • 41
  • 151
  • 191
1
2 3
99 100