Skip to content

๐Ÿ’ก ืžืงื‘ื™ืœื™ื•ืช ื‘ืคื™ื™ืชื•ืŸ โ€“ ืžืชื™ ื•ืœืžื” ืœื”ืฉืชืžืฉ ื‘ืชื”ืœื™ื›ื™ื ื•ืฉืจืฉื•ืจื™ื?

ืžืงื‘ื™ืœื™ื•ืช ื”ื™ื ืื—ืช ื”ื“ืจื›ื™ื ืœืฉืคืจ ื‘ื™ืฆื•ืขื™ื ืฉืœ ืชื•ื›ื ื•ืช ืขืœ ื™ื“ื™ ื”ืจืฆืช ืžืฉื™ืžื•ืช ื‘ืžืงื‘ื™ืœ. ื‘ืคื•ืกื˜ ื”ื–ื” ื ืœืžื“ ืžืชื™ ื ื›ื•ืŸ ืœื”ืฉืชืžืฉ ื‘ึพืฉืจืฉื•ืจื™ื (Threads) ื‘ืคื™ื™ืชื•ืŸ, ื•ื ืจืื” ื“ื•ื’ืžืื•ืช ืžืขืฉื™ื•ืช ื›ื•ืœืœ ื”ืฉื•ื•ืื” ื‘ื™ืŸ ืชื›ื ื™ืช ื—ื“-ืฉืจืฉื•ืจื™ืช ืœื‘ื™ืŸ ืชื›ื ื™ืช ืžืจื•ื‘ืช ืฉืจืฉื•ืจื™ื.


๐Ÿ” ืชื”ืœื™ืš, ืฉืจืฉื•ืจ, ื•ื“ืžื•ืŸ โ€“ ืžื•ืฉื’ื™ื ื‘ืกื™ืกื™ื™ื

  • 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) ืœืžืงืจื™ื ื›ืืœื”.

ื›ืชื™ื‘ืช ืชื’ื•ื‘ื”

ื”ืื™ืžื™ื™ืœ ืœื ื™ื•ืฆื’ ื‘ืืชืจ. ืฉื“ื•ืช ื”ื—ื•ื‘ื” ืžืกื•ืžื ื™ื *