User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Thread Problem

Fri Sep 23, 2016 5:01 pm

First time I've needed to use threads but stumbled at the first hurdle. I thought this code...

Code: Select all

from threading import Thread
import time


def test():
    for i in range(3):
        print i
        time.sleep(1.0)

a = Thread(test())
b = Thread(test())
a.start()
b.start()
...would produce
0
0
1
1
2
2

But I'm getting
0
1
2
0
1
2

Which means they're running one after another, not simultaneously.
Ideas?

Dave.
Apple say... Monkey do !!

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Thread Problem

Fri Sep 23, 2016 6:29 pm

Code: Select all

from threading import Thread
import time


def test():
    for i in range(3):
        print i
        time.sleep(1.0)


a = Thread(target=test)
b = Thread(target=test)

a.start()
b.start()
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Thread Problem

Fri Sep 23, 2016 6:46 pm

I get it now, sorted, thanks.

Dave.
Apple say... Monkey do !!

Return to “Python”