Home > other >  Python threding requests count
Python threding requests count

Time:10-31

When i run this code its show me

1     Hello world

2     Hello world

3     Hello world

4     Hello world

5     Hello world

6     Hello world

7     Hello world

8     Hello world

9     Hello world

10     Hello world

1     Hello world

2     Hello world

3     Hello world

4     Hello world

5     Hello world

6     Hello world

7     Hello world

8     Hello world

9     Hello world

10     Hello world

1     Hello world

2     Hello world

3     Hello world

4     Hello world

5     Hello world

6     Hello world

7     Hello world

8     Hello world

9     Hello world

10     Hello world

[Program finished]

How can i count 1 to 20 not 1 to 10 tow times

from threading import Thread

def test():
    for i in range(10):
        print(str(i 1) "     Hello world")
    
t1=Thread(target=test)
t2=Thread(target=test)

t1.start()

t2.start()

CodePudding user response:

Pass a parameter to your Thread objects to act as a per-thread offset:

def test(offset):
    for i in range(10):
        print(str(offset   i   1)   "    Hello world")

t1 = Thread(test, args=(0,))
t2 = Thread(test, args=(10,))

t1.start()
t2.start()

CodePudding user response:

How can i count 1 to 20 not 1 to 10 tow times

Use a single Thread with a range of 20 ...

>>> def test():
...     for i in range(20):
...         print(str(i 1) "     Hello world")
...
>>> t1=Thread(target=test)
>>> t1.start()
1     Hello world
2     Hello world
3     Hello world
4     Hello world
5     Hello world
6     Hello world
7     Hello world
8     Hello world
9     Hello world
10     Hello world
11     Hello world
12     Hello world
13     Hello world
14     Hello world
15     Hello world
16     Hello world
17     Hello world
18     Hello world
19     Hello world
20     Hello world
  • Related