0

I am solving this question from LeetCode: 1116. Print Zero Even Odd

I am running this solution in VS Code with my own main function to understand the issue in depth.

After reading this question and the suggested solutions. In addition to reading this explanation.

I added this code to the code from the solution:

from threading import Semaphore
import threading

def threaded(fn):
    def wrapper(*args, **kwargs):
        threading.Thread(target=fn, args=args, kwargs=kwargs).start()
    return wrapper

and before those functions from the question I added: @threaded

I added a printNumber function and main function to run it on VS Code.

def printNumber(num):
    print(num, end="")

if __name__ == "__main__":
    a = ZeroEvenOdd(7)
    handle = a.zero(printNumber)
    handle = a.even(printNumber)
    handle = a.odd(printNumber)

Running this code gives me a correct answer but I do not get a new line printed in the terminal after that, I mean for input 7 in my main function, the output is: 01020304050607hostname and not what I want it to be:

01020304050607

hostname

So, I added print("\n") in the main and I saw that I get a random output like:

0102

0304050607

or

0

1020304050607

still without a new line in the end.

When I try to use the join function handle.join() then I get the error:

Exception has occurred: AttributeError 'NoneType' object has no attribute 'join'

I tried to do this:

handle1 = a.zero(printNumber)
handle2 = a.even(printNumber)
handle3 = a.odd(printNumber)
handle1.join()
handle2.join()
handle3.join()

Still got the same error. Where in the code should I do the waiting until the threads will terminate?

Thanks.

philipxy
  • 14,867
  • 6
  • 39
  • 83
Asaf
  • 107
  • 1
  • 12
  • Voting to close because there is not enough information in the question. The answer below explains the meaning of the error message, but OP still seems to be confused, and has not provided enough of a code example to illustrate what is confusing them. – Solomon Slow Oct 25 '21 at 19:09
  • @SolomonSlow But I posted the code I relied on, which is the solution in the link and in addition I posted the code I wrote to run this solution in VS Code. I can post the whole code, that it will be the union of the two above. – Asaf Oct 25 '21 at 21:03

1 Answers1

0

When I try to use...handle.join()...I get the error: "...'NoneType' object has no attribute, 'join'

The error message means that the value of handle was None at the point in your program where your code tried to call handle.join(). There is no join() operation available on the None value.

You probably wanted to join() a thread (i.e., the object returned by Threading.thread(...). For a single thread, you could do this:

t = Threading.thread(...)
t.start()
   ...
t.join()

Your program creates three threads, so you won't be able to just use a single variable t. You could use three separate variables, or you could create a list, or... I'll leave that up to you.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • I edited the question. I tried it before by creating three different variables. It did not help me. – Asaf Oct 25 '21 at 17:37
  • 1
    @Asaf, Why do you think that `a.zero(...)`, `a.even(...)`, and `a.odd(...)` will return thread objects? The code for those functions is not shown in your question, so there's no way for me to guess what you were thinking there. But it certainly looks as if your `wrapper(...)` function creates a thread, starts the thread, and then _throws away the reference to the new thread object._ There's no way you can `join()` a thread if you don't have a reference to the thread object. – Solomon Slow Oct 25 '21 at 17:50
  • the functions: `zero(...), even(...), odd(...)` are the same functions from the solution I attached in the link in question. Why the `wrapper(...)` don't return the thread object? Thanks. – Asaf Oct 25 '21 at 17:59
  • @Asaf Those functions are empty on the page to which you linked: They do nothing, and they return `None`. They are [_skeletons_](https://en.wikipedia.org/wiki/Skeleton_(computer_programming)), that you are supposed to fill in with your own code to make them do something. – Solomon Slow Oct 25 '21 at 18:05
  • I mean from this solution: https://leetcode.com/problems/print-zero-even-odd/discuss/809929/Clean-Python-or-Semaphore-Coordination I have attached this to my question. – Asaf Oct 25 '21 at 18:09