Skip to content

Commit adbdaca

Browse files
committed
python-thread
1 parent 358c027 commit adbdaca

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

taiyangxue/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Python 代码实例
22

3-
- [python-op](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/python-op2) : 只需一招,Python 将系统秒变在线版!
3+
- [python-thread](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/python-thread) : 有了这个方法,数据再多也不怕了
4+
- [python-op2](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/python-op2) : 只需一招,Python 将系统秒变在线版!
45
- [python-op](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/python-op) : YYDS! Python 帮我扛起运营大旗!
56
- [timefriend](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/timefriend) :做时间的朋友 —— 用印象笔记打造时间记录工具
67
- [pythondocx](https://github.com/JustDoPython/python-examples/tree/master/taiyangxue/pythondocx) :Word 神器 python-docx

taiyangxue/python-thread/code.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import time
2+
import threading
3+
4+
class DataSource:
5+
def __init__(self, dataFileName, startLine=0, maxcount=None):
6+
self.dataFileName = dataFileName
7+
self.startLine = startLine # 第一行行号为1
8+
self.line_index = startLine # 当前读取位置
9+
self.maxcount = maxcount # 读取最大行数
10+
self.lock = threading.RLock() # 同步锁
11+
12+
self.__data__ = open(self.dataFileName, 'r', encoding= 'utf-8')
13+
for i in range(self.startLine):
14+
l = self.__data__.readline()
15+
16+
def getLine(self):
17+
self.lock.acquire()
18+
try:
19+
if self.maxcount is None or self.line_index < (self.startLine + self.maxcount):
20+
line = self.__data__.readline()
21+
if line:
22+
self.line_index += 1
23+
return True, line
24+
else:
25+
return False, None
26+
else:
27+
return False, None
28+
29+
except Exception as e:
30+
return False, "处理出错:" + e.args
31+
finally:
32+
self.lock.release()
33+
34+
def __del__(self):
35+
if not self.__data__.closed:
36+
self.__data__.close()
37+
print("关闭数据源:", self.dataFileName)
38+
39+
def process(worker_id, datasource):
40+
count = 0
41+
while True:
42+
status, data = datasource.getLine()
43+
if status:
44+
print(">>> 线程[%d] 获得数据, 正在处理……" % worker_id)
45+
time.sleep(3) # 等待3秒模拟处理过程
46+
print(">>> 线程[%d] 处理数据 完成" % worker_id)
47+
count += 1
48+
else:
49+
break # 退出循环
50+
print(">>> 线程[%d] 结束, 共处理[%d]条数据" % (worker_id, count))
51+
52+
53+
def main():
54+
datasource = DataSource('data.txt') # 创建数据源类
55+
workercount = 10 # 开启的线程数
56+
workers = []
57+
for i in range(workercount):
58+
worker = threading.Thread(target=process, args=(i+1, datasource))
59+
worker.start()
60+
workers.append(worker)
61+
62+
for worker in workers:
63+
worker.join()

taiyangxue/python-thread/data.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
1
2+
2
3+
3
4+
3
5+
4
6+
5
7+
6
8+
7
9+
8
10+
9
11+
10
12+
11
13+
12
14+
13
15+
14
16+
15
17+
16

0 commit comments

Comments
 (0)