ืืงืืืืืืช ืืื ืืืช ืืืจืืื ืืฉืคืจ ืืืฆืืขืื ืฉื ืชืืื ืืช ืขื ืืื ืืจืฆืช ืืฉืืืืช ืืืงืืื. ืืคืืกื ืืื ื ืืื ืืชื ื ืืื ืืืฉืชืืฉ ืึพืฉืจืฉืืจืื (Threads) ืืคืืืชืื, ืื ืจืื ืืืืืืืช ืืขืฉืืืช ืืืื ืืฉืืืื ืืื ืชืื ืืช ืื-ืฉืจืฉืืจืืช ืืืื ืชืื ืืช ืืจืืืช ืฉืจืฉืืจืื.
- ๐ ืชืืืื, ืฉืจืฉืืจ, ืืืืื โ ืืืฉืืื ืืกืืกืืื
- ๐ง ืืชื ื ืฉืชืืฉ ืึพThreading?
- ๐ ืชืื ืืช ืื-ืฉืจืฉืืจืืช โ ืขืืืื ืงืืฆืื ืืื ืืืจื ืืฉื ื
- ๐ ืชืื ืืช ืืจืืืช ืฉืจืฉืืจืื โ ืขืืืื ืงืืฆืื ืืืงืืื
- ๐ฆ ืฉืืืืฉ ืึพQueue ืืขืืืื ืืืืื ืืื ืฉืจืฉืืจืื
- ๐งฉ ืืกืืืื
๐ ืชืืืื, ืฉืจืฉืืจ, ืืืืื โ ืืืฉืืื ืืกืืกืืื
- Process (ืชืืืื) โ ืืืคืข ืฉื ืชืืื ื ืฉืจืฆื ืืืืืจืื.
- Thread (ืฉืจืฉืืจ) โ ืืืืืช ืืืฆืืข ืืชืื ืชืืืื. ืชืืืื ืืื ืืืื ืืืืื ืืกืคืจ ืฉืจืฉืืจืื.
- Daemon Thread (ืฉืจืฉืืจ ืืืื) โ ืฉืจืฉืืจ ืฉืจืฅ ืืจืงืข, ืืืกืชืืื ืืืืืืืืช ืืฉืืชืืืื ืืจืืฉื ืืกืชืืื.
๐ง ืืชื ื ืฉืชืืฉ ืึพThreading?
ืฉืจืฉืืจืื ืืคืืืชืื ืืชืืืืื ืืืืืื ืืืฉืืืืช ืืงืฉืืจืืช ืึพืงืื/ืคืื (I/O) โ ืืืืืื:
- ืฉืืืืช ืืงืฉืืช ืจืฉืช
- ืขืืืื ืขื ืืกืื ื ืชืื ืื
- ืงืจืืื/ืืชืืื ืืงืืฆืื
ืื ืืฉ ืืื ืงืื "ืืื" ืืืฉืืืืช โ ืืืชืื ืฉึพ
multiprocessing
ืชืืื ืืืืจื ืืืื ืืืชืจ. ืืื ืื ืื ื ืืชืืงืืื ืึพthreading
.
๐ ืชืื ืืช ืื-ืฉืจืฉืืจืืช โ ืขืืืื ืงืืฆืื ืืื ืืืจื ืืฉื ื
from time import perf_counter
def replace(filename, substr, new_substr):
print(f'ืขืืืืื ืขื ืืงืืืฅ {filename}')
with open(filename, 'r') as f:
content = f.read()
content = content.replace(substr, new_substr)
with open(filename, 'w') as f:
f.write(content)
def main():
filenames = [
'c:/temp/test1.txt',
'c:/temp/test2.txt',
'c:/temp/test3.txt',
'c:/temp/test4.txt',
'c:/temp/test5.txt',
'c:/temp/test6.txt',
'c:/temp/test7.txt',
'c:/temp/test8.txt',
'c:/temp/test9.txt',
'c:/temp/test10.txt',
]
for filename in filenames:
replace(filename, 'ids', 'id')
if __name__ == "__main__":
start_time = perf_counter()
main()
end_time = perf_counter()
print(f'ืืขืืืื ืืงื {end_time - start_time:.2f} ืฉื ืืืช.')
๐ ืชืื ืืช ืืจืืืช ืฉืจืฉืืจืื โ ืขืืืื ืงืืฆืื ืืืงืืื
from threading import Thread
from time import perf_counter
def replace(filename, substr, new_substr):
print(f'ืขืืืืื ืขื ืืงืืืฅ {filename}')
with open(filename, 'r') as f:
content = f.read()
content = content.replace(substr, new_substr)
with open(filename, 'w') as f:
f.write(content)
def main():
filenames = [
'c:/temp/test1.txt',
'c:/temp/test2.txt',
'c:/temp/test3.txt',
'c:/temp/test4.txt',
'c:/temp/test5.txt',
'c:/temp/test6.txt',
'c:/temp/test7.txt',
'c:/temp/test8.txt',
'c:/temp/test9.txt',
'c:/temp/test10.txt',
]
threads = [Thread(target=replace, args=(filename, 'id', 'ids')) for filename in filenames]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
if __name__ == "__main__":
start_time = perf_counter()
main()
end_time = perf_counter()
print(f'ืืขืืืื ืืงื {end_time - start_time:.2f} ืฉื ืืืช.')
๐ ืฉืืื ืื: ืืขืืืื ืขื ืงืืฆืื โ ืืืื ืฉืืื ืืชื ืืฉืืืืช ืืืฉื ืืงืืืฅ ืืื ืืื ืืข ืชืงืืืช ืืชืืื.
๐ฆ ืฉืืืืฉ ืึพQueue ืืขืืืื ืืืืื ืืื ืฉืจืฉืืจืื
ืืื ืืืขืืืจ ืืืืข ืืื ืฉืจืฉืืจืื ืืฆืืจื ืืืืื, ื ืฉืชืืฉ ืืืืืงืช Queue
. ืืืืื ืงืืืกืืช โ ืืคืืง ืืฆืจืื:
import time
from queue import Empty, Queue
from threading import Thread
def producer(queue):
for i in range(1, 6):
print(f'ืืื ืืกืื ืืช ืืคืจืื {i} ืืชืืจ')
time.sleep(1)
queue.put(i)
def consumer(queue):
while True:
try:
item = queue.get()
except Empty:
continue
else:
print(f'ืืขืืืื ืืช ืืคืจืื {item}')
time.sleep(2)
queue.task_done()
def main():
queue = Queue()
producer_thread = Thread(target=producer, args=(queue,))
producer_thread.start()
consumer_thread = Thread(target=consumer, args=(queue,), daemon=True)
consumer_thread.start()
producer_thread.join()
queue.join()
if __name__ == '__main__':
main()
๐งฉ ืืกืืืื
- ืื ืืชื ืขืืืืื ืขื ืงืื/ืคืื โ ืฉืจืฉืืจืื ืืืืืื ืืฉืคืจ ืืืฆืืขืื ืืฉืืขืืชืืช.
- ืืฉืชืืฉื ืึพ
Thread
ืึพQueue
ืืฉืืฉ ืฆืืจื ืืืจืืฅ ืืฉืืืืช ืืืงืืื ืืืืื. - ืืืืืจื ืืฉืืืืฉ ืึพthreads ืืืืฉืืืื ืืืืื โ ืคืืืชืื ืืืืื ืึพGIL (ื ืขืื ืืืืืื) ืืืื ืขืืืฃ ืชืืืืืื (
multiprocessing
) ืืืงืจืื ืืืื.