Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionError when threading in Python

I'm trying to run some simple threading in Python using:

t1 = threading.Thread(analysis("samplequery")) t1.start()  other code runs in here  t1.join() 

Unforunately I'm getting the error:

"AssertionError: group argument must be none for now"

I've never implemented threading in Python before, so I'm a bit unsure as to what's going wrong. Does anyone have any idea what the problem is?

I'm not sure if it's relevant at all, but analysis is a method imported from another file.

I had one follow up query as well. Analysis returns a dictionary, how would I go about assigning that for use in the original method?

Thanks

like image 433
djcmm476 Avatar asked Mar 11 '13 22:03

djcmm476


People also ask

Is threading in python parallel?

In fact, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. This limitation is actually enforced by GIL. The Python Global Interpreter Lock (GIL) prevents threads within the same process to be executed at the same time.

How do you use mutex in Python?

A mutex lock can be used to ensure that only one thread at a time executes a critical section of code at a time, while all other threads trying to execute the same code must wait until the currently executing thread is finished with the critical section and releases the lock.


1 Answers

You want to specify the target keyword parameter instead:

t1 = threading.Thread(target=analysis("samplequery")) 

You probably meant to make analysis the run target, but 'samplequery the argument when started:

t1 = threading.Thread(target=analysis, args=("samplequery",)) 

The first parameter to Thread() is the group argument, and it currently only accepts None as the argument.

From the threading.Thread() documentation:

This constructor should always be called with keyword arguments. Arguments are:

  • group should be None; reserved for future extension when a ThreadGroup class is implemented.
  • target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
like image 99
Martijn Pieters Avatar answered Sep 22 '22 21:09

Martijn Pieters