0

I'm attempting to thread a function call in my Python catastr^H^H^H^H^H^Hreation, and I've read up on how to use the threading.Thread() call. My function takes a simple string argument, so theoretically it should be as easy as:

thread = threading.Thread(target = my_func, args = (string_var, )) bearing in mind that the args() needs to be a tuple. Got it. However, it appears as though I'm still doing something wrong because I continually get the barffage from Python:

TypeError: my_func() takes 1 positional argument but 2 were given

I'm a bit stumped here. Any guidance?

Thanks!

Jason VP
  • 1
  • 1
  • Can you show a little bit more code? Is it a function or a method? – gribvirus74 Feb 26 '21 at 16:32
  • You'll have to forgive that I can't since it's internal stuff. It's a method though. It's simply calling the Python container API to restart a container. The string being passed in is the name of said container. Functionally it works fine until I attempt to thread it. Grr! – Jason VP Feb 26 '21 at 16:36

3 Answers3

0

please provide some code for us to help you. But before you do your post could be a possible duplicate of this post.

0

Seems the issue is that because it's a method (thanks gribvirus74 for the idea) and I'm attempting to thread it, it won't inherit the self. And that appears to be the issue. I moved the function outside of the class and called it with the Thread(). Works fine now.

Jason VP
  • 1
  • 1
0

If it's a method, then you can write the following code (assuming the class name is SomeClass and it has a method called foo with one argument):

x = SomeClass()

thread = threading.Thread(target=SomeClass.foo, args=(x, 'your method argument'))
gribvirus74
  • 755
  • 6
  • 20
  • Right, but I was trying this from within another method in the same class. That's what was fouling me up. – Jason VP Feb 26 '21 at 19:33