{"id":340,"date":"2025-03-10T04:14:34","date_gmt":"2025-03-10T04:14:34","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=340"},"modified":"2025-03-10T04:14:34","modified_gmt":"2025-03-10T04:14:34","slug":"thread-trong-python-lap-trinh-da-luong-mot-cach-de-dang","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=340","title":{"rendered":"Thread trong Python: L\u1eadp tr\u00ecnh \u0111a lu\u1ed3ng m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"598\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-21.png\" alt=\"\" class=\"wp-image-341\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-21.png 1024w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-21-300x175.png 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-21-768x449.png 768w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-21-850x496.png 850w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Trong Python, <strong>Thread<\/strong> (lu\u1ed3ng) l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt gi\u00fap ch\u01b0\u01a1ng tr\u00ecnh th\u1ef1c thi nhi\u1ec1u t\u00e1c v\u1ee5 c\u00f9ng l\u00fac, t\u1ed1i \u01b0u h\u00f3a th\u1eddi gian x\u1eed l\u00fd. \u0110i\u1ec1u n\u00e0y \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 nh\u01b0 t\u1ea3i xu\u1ed1ng nhi\u1ec1u file, x\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn ho\u1eb7c t\u01b0\u01a1ng t\u00e1c v\u1edbi API m\u00e0 kh\u00f4ng l\u00e0m ch\u1eadm ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Gi\u1edbi thi\u1ec7u v\u1ec1 Thread trong Python<\/strong><\/h2>\n\n\n\n<p>Python h\u1ed7 tr\u1ee3 \u0111a lu\u1ed3ng th\u00f4ng qua module <code>threading<\/code>. Khi s\u1eed d\u1ee5ng <strong>thread<\/strong>, ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 ch\u1ea1y nhi\u1ec1u t\u00e1c v\u1ee5 \u0111\u1ed3ng th\u1eddi m\u00e0 kh\u00f4ng c\u1ea7n ch\u1edd t\u1eebng t\u00e1c v\u1ee5 ho\u00e0n th\u00e0nh tr\u01b0\u1edbc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>T\u1ea1i sao n\u00ean d\u00f9ng Thread?<\/strong><\/h3>\n\n\n\n<p>\u2705 <strong>C\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t<\/strong>: Ch\u1ea1y nhi\u1ec1u t\u00e1c v\u1ee5 song song thay v\u00ec tu\u1ea7n t\u1ef1.<br>\u2705 <strong>T\u0103ng t\u1ed1c \u0111\u1ed9 x\u1eed l\u00fd<\/strong>: H\u1eefu \u00edch khi x\u1eed l\u00fd t\u00e1c v\u1ee5 I\/O nh\u01b0 \u0111\u1ecdc file, g\u1eedi HTTP request.<br>\u2705 <strong>Gi\u1eef giao di\u1ec7n m\u01b0\u1ee3t m\u00e0<\/strong>: Khi l\u1eadp tr\u00ecnh GUI, s\u1eed d\u1ee5ng Thread gi\u00fap giao di\u1ec7n kh\u00f4ng b\u1ecb \u0111\u01a1.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. C\u00e1ch t\u1ea1o v\u00e0 ch\u1ea1y Thread<\/strong><\/h2>\n\n\n\n<p>Python cung c\u1ea5p 2 c\u00e1ch t\u1ea1o thread:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>D\u00f9ng <code>threading.Thread<\/code> v\u1edbi h\u00e0m c\u00f3 s\u1eb5n<\/strong><\/li>\n\n\n\n<li><strong>D\u00f9ng <code>threading.Thread<\/code> v\u1edbi class k\u1ebf th\u1eeba t\u1eeb <code>Thread<\/code><\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C\u00e1ch 1: S\u1eed d\u1ee5ng <code>threading.Thread<\/code> v\u1edbi h\u00e0m<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import threading<br>import time<br><br>def say_hello(name):<br>    for i in range(5):<br>        print(f\"Hello, {name}! - {i}\")<br>        time.sleep(1)<br><br># T\u1ea1o v\u00e0 kh\u1edfi ch\u1ea1y lu\u1ed3ng<br>thread1 = threading.Thread(target=say_hello, args=(\"Alice\",))<br>thread2 = threading.Thread(target=say_hello, args=(\"Bob\",))<br><br>thread1.start()<br>thread2.start()<br><br>thread1.join()<br>thread2.join()<br><br>print(\"T\u1ea5t c\u1ea3 thread \u0111\u00e3 ho\u00e0n th\u00e0nh!\")<br><\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>threading.Thread(target=say_hello, args=(\"Alice\",))<\/code>: T\u1ea1o thread m\u1edbi ch\u1ea1y h\u00e0m <code>say_hello<\/code> v\u1edbi tham s\u1ed1 <code>\"Alice\"<\/code>.<\/li>\n\n\n\n<li><code>.start()<\/code>: B\u1eaft \u0111\u1ea7u th\u1ef1c thi lu\u1ed3ng.<\/li>\n\n\n\n<li><code>.join()<\/code>: \u0110\u1ee3i thread ho\u00e0n th\u00e0nh tr\u01b0\u1edbc khi ti\u1ebfp t\u1ee5c ch\u01b0\u01a1ng tr\u00ecnh ch\u00ednh.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C\u00e1ch 2: K\u1ebf th\u1eeba <code>Thread<\/code> \u0111\u1ec3 t\u1ea1o lu\u1ed3ng<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import threading<br>import time<br><br>class MyThread(threading.Thread):<br>    def __init__(self, name):<br>        threading.Thread.__init__(self)<br>        self.name = name<br><br>    def run(self):<br>        for i in range(5):<br>            print(f\"{self.name} \u0111ang ch\u1ea1y - {i}\")<br>            time.sleep(1)<br><br># T\u1ea1o v\u00e0 ch\u1ea1y nhi\u1ec1u thread<br>thread1 = MyThread(\"Lu\u1ed3ng 1\")<br>thread2 = MyThread(\"Lu\u1ed3ng 2\")<br><br>thread1.start()<br>thread2.start()<br><br>thread1.join()<br>thread2.join()<br><br>print(\"Ho\u00e0n th\u00e0nh t\u1ea5t c\u1ea3 c\u00e1c lu\u1ed3ng!\")<br><\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>K\u1ebf th\u1eeba <code>threading.Thread<\/code> v\u00e0 override ph\u01b0\u01a1ng th\u1ee9c <code>run()<\/code>.<\/li>\n\n\n\n<li>M\u1ed7i thread s\u1ebd ch\u1ea1y ph\u01b0\u01a1ng th\u1ee9c <code>run()<\/code> khi <code>start()<\/code> \u0111\u01b0\u1ee3c g\u1ecdi.<\/li>\n\n\n\n<li>D\u00f9ng <code>.join()<\/code> \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u1ea5t c\u1ea3 thread k\u1ebft th\u00fac tr\u01b0\u1edbc khi ti\u1ebfp t\u1ee5c ch\u01b0\u01a1ng tr\u00ecnh.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. S\u1eed d\u1ee5ng Lock \u0111\u1ec3 tr\u00e1nh xung \u0111\u1ed9t d\u1eef li\u1ec7u<\/strong><\/h2>\n\n\n\n<p>Khi nhi\u1ec1u thread c\u00f9ng truy c\u1eadp v\u00e0 ch\u1ec9nh s\u1eeda m\u1ed9t t\u00e0i nguy\u00ean chung, c\u00f3 th\u1ec3 x\u1ea3y ra <strong>race condition<\/strong> (xung \u0111\u1ed9t d\u1eef li\u1ec7u). \u0110\u1ec3 tr\u00e1nh \u0111i\u1ec1u n\u00e0y, s\u1eed d\u1ee5ng <strong>Lock<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import threading<br><br>counter = 0<br>lock = threading.Lock()<br><br>def increment():<br>    global counter<br>    for _ in range(100000):<br>        with lock:<br>            counter += 1<br><br>thread1 = threading.Thread(target=increment)<br>thread2 = threading.Thread(target=increment)<br><br>thread1.start()<br>thread2.start()<br><br>thread1.join()<br>thread2.join()<br><br>print(f\"Counter cu\u1ed1i c\u00f9ng: {counter}\")<br><\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>lock = threading.Lock()<\/code>: T\u1ea1o kh\u00f3a (Lock).<\/li>\n\n\n\n<li><code>with lock:<\/code>: \u0110\u1ea3m b\u1ea3o ch\u1ec9 c\u00f3 m\u1ed9t thread truy c\u1eadp <code>counter<\/code> t\u1ea1i m\u1ed9t th\u1eddi \u0111i\u1ec3m.<\/li>\n\n\n\n<li>N\u1ebfu kh\u00f4ng c\u00f3 <strong>Lock<\/strong>, <code>counter<\/code> c\u00f3 th\u1ec3 kh\u00f4ng \u0111\u1ea1t gi\u00e1 tr\u1ecb mong \u0111\u1ee3i do race condition.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng Thread?<\/strong><\/h2>\n\n\n\n<p>\u2705 Khi x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 <strong>I\/O-bound<\/strong> nh\u01b0:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>G\u1eedi request HTTP\/API.<\/li>\n\n\n\n<li>\u0110\u1ecdc\/ghi file ho\u1eb7c database.<\/li>\n\n\n\n<li>T\u1ea3i nhi\u1ec1u h\u00ecnh \u1ea3nh c\u00f9ng l\u00fac.<\/li>\n<\/ul>\n\n\n\n<p>\u274c Kh\u00f4ng n\u00ean d\u00f9ng Thread khi x\u1eed l\u00fd <strong>CPU-bound<\/strong> (t\u00e1c v\u1ee5 n\u1eb7ng nh\u01b0 x\u1eed l\u00fd \u1ea3nh, AI).<br>\ud83d\udd39 Thay v\u00e0o \u0111\u00f3, h\u00e3y s\u1eed d\u1ee5ng <code>multiprocessing<\/code> \u0111\u1ec3 t\u1eadn d\u1ee5ng nhi\u1ec1u CPU core.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trong Python, Thread (lu\u1ed3ng) l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt gi\u00fap ch\u01b0\u01a1ng tr\u00ecnh th\u1ef1c thi nhi\u1ec1u t\u00e1c v\u1ee5 c\u00f9ng l\u00fac, t\u1ed1i \u01b0u h\u00f3a th\u1eddi gian x\u1eed l\u00fd. \u0110i\u1ec1u n\u00e0y \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/340\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/340\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}