1

I recently started to study threads and I know it is about multiple processing stuff I just don't get why this is happening

import threading

class BuckyMessenger(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        print("test")

    def run(self):
        for _ in range(4):
            print(threading.current_thread().getName())

    x = BuckyMessenger(name='Send')
y = BuckyMessenger(name='Receive')
z = BuckyMessenger(name='Nothing')
x.start()
y.start()
z.start()

I Expected this to happen:

test
Send
test
Receive
test
Nothing

from here on, I should have gotten 9 more prints of 3 random "Send" and 3 random "Receive" and 3 Random "Nothing", like the following: (rest of the results)

Send
Receive 
Nothing 
Nothing
Nothing
Send
Receive 
Receive 
send

But this is the result I got: I mean why? why python acts this way?

test
test
test
Thread-1
Thread-1
Thread-1
Thread-1
Thread-2
Thread-2
Thread-2
Thread-2
Thread-3
Thread-3
Thread-3
Thread-3

2 Answers2

0

You need to get a reference to the current thread running and set its name. Then you can use that reference to print its name. Notice the changes in constructor and run method. As for the effect of serial execution, is due to the ability of each thread to be executed fast enough and finish before another takes place, because of the small amount of loop cycles:

import threading

class BuckyMessenger(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
        print("test")

    def run(self):
        curThread = threading.current_thread()
        curThread.name = self.name
        for _ in range(4):
            print(curThread.name)

x = BuckyMessenger(name='Send')
y = BuckyMessenger(name='Receive')
z = BuckyMessenger(name='Nothing')
x.start()
y.start()
z.start()

Possible Output:

test
test
test
Send
Send
Send
Send
Receive
Nothing
Nothing
Receive
Nothing
Nothing
Receive
Receive
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • test test test Send Send Send Send Receive Receive Receive Receive Nothing Nothing Nothing Nothing, that's the result every time , is it threading? since I'm not getting a mixed of "Receive" and "Nothing" and "Send" – Parsa Fakhar Nov 08 '18 at 14:17
  • It's because of the ability of CPU to execute each thread quick enough so it looks like serial execution. One thing you can do is to increase the number of loop cycles and another is to force each thread to suspend for a randomly generated amount of time e.g. `time.sleep(random.random())`. Make sure to import the `time` and `random` modules for the latter option. Place `time.sleep(random.random())` in the `for` loop, before the `print` statement. – Vasilis G. Nov 08 '18 at 16:44
0

Your initializer does not set the passed name to the BuckyMessanger. This is because you override the original Thread.__init__(), which handles name as you would expect. The default names for the threads are Thread-1 and so on, in the respective order they are created. Change your initializer to something like the following to override the defaults:

def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
        print("test")

As for why they are printed in that order, BuckyMessanger.run() runs after each start() and finishes before the next start() is called.