58

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

djcmm476
  • 1,723
  • 6
  • 24
  • 46

2 Answers2

88

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.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This still passes the result of `analysis` to the Thread constructor, which is wrong unless the function returns a callable. – g.d.d.c Mar 11 '13 at 22:38
  • @g.d.d.c: Yeah, was getting to that. :-) – Martijn Pieters Mar 11 '13 at 22:40
  • No problem, just making sure. :) – g.d.d.c Mar 11 '13 at 22:41
  • That seems to fix the problem, thanks! How would I access the dictionary though, I'm still not entirely sure how to get to it? – djcmm476 Mar 11 '13 at 22:46
  • @Incredidave: You need to communicate among your threads (main and sub threads), perhaps using a queue; see [python multithreading for dummies](http://stackoverflow.com/q/2846653) and any good threading and python tutorial. – Martijn Pieters Mar 11 '13 at 22:48
  • @MartijnPieters Managed to get it working using a queue, thanks a lot for the help and links. – djcmm476 Mar 11 '13 at 22:59
  • I had the same with the `Process()` constructor. – Valentin Grégoire Jun 08 '20 at 11:10
  • @ValentinGrégoire: yes, the [`multiprocessing.Process()` class](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process) is deliberately modelled on the `threading.Thread()` class and has the name behaviour when it comes to the first argument. – Martijn Pieters Jun 11 '20 at 12:13
9

You need to provide the target attribute:

t1 = threading.Thread(target = analysis, args = ('samplequery',))
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111