python simple threading and program learning

Keywords: Python network

The support for threads in Python is not enough, but it is said that Python has a complete asynchronous network framework module. I hope I can learn it in the future. Here is a brief summary of threads in python.

The threading library can be used to execute any python callable object in a separate thread. Although this module does not support thread-related operations enough, we can use simple threads to handle I/O operations to reduce program response time.

from threading import Thread
import time


def countdown(n):
    while n > 0:
        print('T-minus:', n)
        n -= 1


t = Thread(target=countdown, args=(10,))
t.start()  # Open threads

time.sleep(2)

if t.is_alive() is True:
    print("Stop threads...")
    t._stop()  # Stop threads

The start function is used to open threads and the _stop function is used to stop threads. To prevent blocking during I/O operations in threads, after running for a period of time, you can determine whether threads are still alive. If threads still exist, call _stop() to stop and prevent blocking (you can encapsulate the _stop function into classes, which I did not do here).

Of course, instead of creating threads manually, you can call the ThreadPool thread pool to process them. If there is no need to share variables among threads, it is convenient to use threads, which can reduce a lot of troublesome operations and save time. If we need to communicate between threads, we can use queues to achieve:

from queue import Queue
from threading import Thread


class kill:
    def terminate(self, t):
        if t.isAlive is True:
            t._stop()


def product(out_q):
    for i in range(5):
            out_q.put(i)


def consumer(in_q):
    for i in range(5):
        print(in_q.get())


q = Queue()
t1 = Thread(target=consumer, args=(q,))
t2 = Thread(target=product, args=(q,))
t1.start()
t2.start()


k = kill()  # Query whether the thread terminates to prevent blocking.
k.terminate(t1)
k.terminate(t2)

  

Queue instances are shared by all threads, and they have all the locks needed, so they can be safely shared among any number of threads. Note here that queue class methods other than put(),get() methods are no longer used in multithreading, because this is not reliable in a multithreaded environment! Queues can be used for data communication in simple, small threads. If large data needs interactive communication, python provides relevant modules that you can use, specific u need baidu.

The so-called coroutine is actually a yield program in a single-threaded environment.

from collections import deque


def countdown(n):
    while n > 0:
        print("T-minus", n)
        yield  # The next time you go back, you can execute it directly from here....Amount to C#It has yield return.
        n -= 1
    print("this is countdown!!!")


def countup(n):
    x = 0
    while x < n:
        print("Counting up", x)
        yield
        x += 1


class TaskScheduler:
    def __init__(self):
        self._task_queue = deque()

    def new_task(self, task):
        self._task_queue.append(task)

    def run(self):
        while self._task_queue:
            task = self._task_queue.popleft()
            try:
                next(task)
                self._task_queue.append(task)
            except StopIteration:
                pass


sche = TaskScheduler()
sche.new_task(countdown(10))
sche.new_task(countdown(5))
sche.new_task(countup(15))
sche.run()

Python is really good, but its performance is also criticized. At first, learning python, I also do some dazzling programs. At least it sounds superb, such as using python's natural language processing to do emotional analysis and the hottest crawler program, as well as dazzling data analysis charts. Gradually, I put those down, because the focus of the program is not those, as long as you can point to the basic grammar, understand the official documents can be made, and the focus of the program code is performance, optimization. Write the program with the most perfect function, the best performance and the best structure. In fact, it is a little like the "cultural soft power" often said by political teachers. The "soft power" in the program should embeds the most suitable design mode, optimizes the program and adopts the most performance-saving data structure.

Posted by barrowvian on Sat, 22 Jun 2019 13:51:36 -0700