有如下代码:
for i in range(1,11,2):
print(i)
这里的范围是1到10,运行后输出为1,3,5,7,9。假如我想实现输出为1,3,5,7,9,10的效果,应该怎么做呢?
有如下代码:
for i in range(1,11,2):
print(i)
这里的范围是1到10,运行后输出为1,3,5,7,9。假如我想实现输出为1,3,5,7,9,10的效果,应该怎么做呢?
import threading
import time
class Worker(threading.Thread):
def __init__(self, stage):
threading.Thread.__init__(self)
self.stage = stage
def run(self):
for i in xrange( self.stage, 158, 10 ):
print "%s : %d" %( self.getName, i )
for i in xrange( 1, 11 ):
worker = Worker( i )
worker.start()
time.sleep( 1 )
如下是可能的输出结果,因为是多线程,顺序不确定,如下做了排序,以验证处理了1~157:
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 1
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 2
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 3
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 4
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 5
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 6
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 7
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 8
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 9
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 10
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 11
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 12
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 13
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 14
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 15
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 16
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 17
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 18
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 19
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 20
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 21
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 22
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 23
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 24
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 25
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 26
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 27
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 28
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 29
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 30
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 31
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 32
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 33
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 34
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 35
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 36
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 37
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 38
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 39
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 40
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 41
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 42
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 43
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 44
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 45
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 46
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 47
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 48
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 49
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 50
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 51
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 52
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 53
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 54
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 55
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 56
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 57
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 58
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 59
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 60
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 61
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 62
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 63
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 64
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 65
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 66
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 67
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 68
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 69
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 70
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 71
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 72
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 73
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 74
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 75
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 76
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 77
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 78
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 79
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 80
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 81
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 82
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 83
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 84
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 85
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 86
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 87
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 88
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 89
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 90
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 91
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 92
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 93
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 94
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 95
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 96
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 97
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 98
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 99
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 100
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 101
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 102
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 103
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 104
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 105
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 106
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 107
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 108
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 109
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 110
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 111
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 112
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 113
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 114
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 115
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 116
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 117
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 118
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 119
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 120
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 121
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 122
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 123
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 124
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 125
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 126
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 127
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 128
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 129
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 130
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 131
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 132
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 133
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 134
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 135
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 136
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 137
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 138
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 139
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 140
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 141
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 142
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 143
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 144
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 145
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 146
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 147
<bound method Worker.getName of <Worker(Thread-8, started 4453552128)>> : 148
<bound method Worker.getName of <Worker(Thread-9, started 4449345536)>> : 149
<bound method Worker.getName of <Worker(Thread-10, started 4457758720)>> : 150
<bound method Worker.getName of <Worker(Thread-1, started 4445138944)>> : 151
<bound method Worker.getName of <Worker(Thread-2, started 4449345536)>> : 152
<bound method Worker.getName of <Worker(Thread-3, started 4453552128)>> : 153
<bound method Worker.getName of <Worker(Thread-4, started 4449345536)>> : 154
<bound method Worker.getName of <Worker(Thread-5, started 4457758720)>> : 155
<bound method Worker.getName of <Worker(Thread-6, started 4461965312)>> : 156
<bound method Worker.getName of <Worker(Thread-7, started 4445138944)>> : 157
4 回答4.4k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
1 回答4.4k 阅读✓ 已解决
1 回答3.9k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决