简体   繁体   中英

thread in python vs C# vs Ruby

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?

Well, actually you can:

>>> import threading
>>> def target():
...     print 123
...
>>> threading.Thread(target=target).start()
123

Your example would look like next:

>>> def target(i):
...     print i
...
>>> for i in xrange(100):
...     threading.Thread(target=target, args=(i,)).start()
...
0
1
2
3
<and so on>

In Python:

import threading

def action(arg):
    print arg

for i in range(100):
    threading.Thread(target=action, args=(i,)).start()

If you want the different threads to sometimes see the same value of i then you need to pass in a mutable object and mutate it:

import threading

def action(arg):
    print arg[0],

mutable = [0]
for i in range(100):
    mutable[0] = i
    threading.Thread(target=action, args=(mutable,)).start()

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:

0 1 2 3 5 5 6 8 9 9 11 11 12 1314 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 3
0 31 32 33 34 35 36 37 39 39 40 41 43 44 44 45 46 47 48 49 50 51 53 53 54 55 56
5758 59 60 61 62 64 65 6666 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8
4 85 86 87 89 90 90 92 93 93 94 95 96 9898 99

Edited again:

I think I've found something which C# and Ruby can do but Python can not

No this has duplicate numbers and looks very much the same like your Ruby/C# code.

import threading
import sys

for i in xrange(100):
    threading.Thread(target=lambda: sys.stdout.write('{0}\n'.format(i))).start()

-

14
15
17
17
18
19
20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM