J
J
Jungles2020-08-28 19:33:12
Python
Jungles, 2020-08-28 19:33:12

Why do streams in the console work differently than in the terminal?

the simplest example

spoiler
from queue import Queue
from threading import Thread

def worker(q,n):
   while True:
      item = q.get()
      if item is None:
         break
      print(item , n)  

c = Queue()
th1 =Thread(target = worker,args = (c,1))
th2 =Thread(target = worker,args = (c,2))
th1.start()
th2.start()


for i in range(50):
   c.put(i)
      

c.put(None)
th1.join()

c.put(None)
th2.join()


Output in the terminal (what we need)
spoiler
0 1
1 2
2 1
3 2
4 2
5 1
6 2
7 1
8 2
9 1
10 2
11 1
12 2
13 1
14 2
15 1
16 2
17 1
18 2
19 1
20 2
21 1
22 2
23 1
24 2
25 1
26 2
27 1
28 2
29 1
30 2
31 1
33 1
32 2
34 1
35 2
36 1
37 2
38 1
39 2
40 1
41 2
42 1
43 2
44 1
45 2
46 1
47 2
48 1
49 2


but the output in the console works somehow inadequately.
spoiler
01 12

23 12

45 12

67 12

89 12

1011 12

1213 12

1415 12

1617 12

1819 12

2021 12

2223 12

2425 12

2627 12

2829 12

3031 12

3233 12

3435 12

3637 12

3839 12

4041 12

4243 12

4445 12

4647 12

4849 12

Why?

PS: I want to clarify what changes from the Queue (max_size) parameter?
I do not really understand the definition of the maximum number of objects in the queue.
1 or 10 objects, depending on it.
assumed that if q = Queue(2),
then let's say
1 1
2 1
3 2
4 2,
but it's still different, for example
1 1
2 2
3 1
4 2

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question