- 论坛徽章:
- 11
|
回复 1# bh3531
属于传参错误,看文档- class threading.Timer(interval, function, args=[], kwargs={})
- Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed.
复制代码 一个例子,传参用list- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sys
- import time
- import threading
- def counter(identity, limit):
- for n in range(limit):
- # stderr无缓存,避免线程间交叉输出
- sys.stderr.write('id = {}, cnt = {}\n'.format(identity, n))
- # print 'id = {}, cnt = {}'.format(identity, n)
- time.sleep(0.5)
- def main():
- for i in range(3):
- t = threading.Timer(3, counter, [i, i + 3])
- t.start()
- if __name__ == '__main__':
- main()
复制代码 Python2.7执行,可能输出- id = 0, cnt = 0
- id = 2, cnt = 0
- id = 1, cnt = 0
- id = 2, cnt = 1
- id = 0, cnt = 1
- id = 1, cnt = 1
- id = 2, cnt = 2
- id = 0, cnt = 2
- id = 1, cnt = 2
- id = 1, cnt = 3
- id = 2, cnt = 3
- id = 2, cnt = 4
复制代码 |
|