I think I’ve found something which C# and Ruby can do but Python can not
in C#:
for (var i = 0; i < 100; i++)
new Thread(() => { Console.Write(i+" "); }).Start();
Console.ReadLine();
in Ruby:
for i in 0...100
Thread.start{
print i + ''
}
end
gets
Any ideas? Could you rewrite the above code in python? the output should be similar to C# and Ruby: has duplicate numbers.
Edit
The output is 100 times of print, with duplicate numbers. If you define a class or a new method, you only get 100 times of print without duplicate numbers. I know getting duplicate numbers is meaningless, I just want to use python to achieve the same effect as C# and ruby.
sample output of C#:
3 3 3 3 5 7 8 8 10 10 12 12 13 14 17 17 17 18 20 20 22 23 24 24 25 28 28 29 29 3
1 31 33 33 34 36 38 38 38 41 41 41 42 44 45 45 46 49 49 50 50 52 52 55 56 56 56
58 59 59 60 61 64 64 64 66 66 67 68 69 72 72 72 73 74 76 76 78 78 81 81 82 83 84
84 86 86 87 89 89 90 93 94 94 95 95 97 97 99 99 100
sample output of Ruby:
99999999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999
Why do I want duplicate numbers:
the output of the C# and ruby code have duplicate numbers. That’s because, the main thread are changing the value of i, so some children threads might print the same number. Main thread and the children threads are referencing to the same variable i.
Sometimes this is useful: the main thread changes the variable, the children threads all get updated variable.
But How could we do this in Python?
If you want the different threads to sometimes see the same value of
ithen you need to pass in a mutable object and mutate it:This has nothing whatsoever to do with whether or not you define a separate class for the thread, it is purely down to how you share the value between the threads. A global variable would work to mess up your threading in the same way.
Sample output: