多线程编程、 Linux 编程

refrence

https://leetcode-cn.com/problems/print-in-order/solution/an-xu-da-yin-by-leetcode/

准备工具:
1.Threading Lock
就是一把锁,acquire调用,如果没有资源进入等待
release释放。
1.Threading Thread
然后用Thread来创建进程,start()跑进程
多线程的创建可以用for循环,也可以


class Foo:
    def __init__(self):
        self.firstJobDone = Lock()
        self.secondJobDone = Lock()
        self.ThirdJobDone = Lock()
        self.firstJobDone.acquire()
        self.secondJobDone.acquire()
        # self.ThirdJobDone.acquire()

    def first(self, printFirst: 'Callable[[], None]') -> None:
        # printFirst() outputs "first".
        self.ThirdJobDone.acquire()
        printFirst()
        # Notify the thread that is waiting for the first job to be done.
        self.firstJobDone.release()

    def second(self, printSecond: 'Callable[[], None]') -> None:
        # Wait for the first job to be done
        self.firstJobDone.acquire()
        # printSecond() outputs "second".
        printSecond()
        # Notify the thread that is waiting for the second job to be done.
        self.secondJobDone.release()

    def third(self, printThird: 'Callable[[], None]') -> None:

        # Wait for the second job to be done.
        self.secondJobDone.acquire()
        # printThird() outputs "third".
        printThird()
        self.ThirdJobDone.release()

def printFirst():
    print("one", end="")

def printSecond():
    print("two", end="")

def printThird():
    print("three", end="")

if __name__ == '__main__':
    obj = Foo()
    for i in range(500):
        Thread(target=obj.second, args=(printSecond,)).start()
        Thread(target=obj.third, args=(printThird,)).start()
        Thread(target=obj.first, args=(printFirst,)).start()

Linux 编程
python -c ‘’
抱歉,我只会这个

ps 多进程,一个坑,以后补上。

# https://blog.csdn.net/iterate7/article/details/102883956
from multiprocessing import Pool, Process, managers

def f(x):
    return x*x

data = [1,2,3,4,5,6,9,7,8]
def task_multiprocessing():
    with Pool(4) as p:
        ret = p.map(f, data)
        print(ret)

import requests
from concurrent.futures import ThreadPoolExecutor

urls = [
  'http://www.python.org',
  'https://docs.python.org/3/',
  'https://docs.python.org/3/whatsnew/3.7.html',
  'https://docs.python.org/3/tutorial/index.html',
  'https://docs.python.org/3/library/index.html',
  'https://docs.python.org/3/reference/index.html',
  'https://docs.python.org/3/using/index.html',
  'https://docs.python.org/3/howto/index.html',
  'https://docs.python.org/3/installing/index.html',
  'https://docs.python.org/3/distributing/index.html',
  'https://docs.python.org/3/extending/index.html',
  'https://docs.python.org/3/c-api/index.html',
  'https://docs.python.org/3/faq/index.html'
  ]

def task_multithread():
    with ThreadPoolExecutor(4) as executor:
        results = executor.map(requests.request, 'GET', urls)
        for i in results:
            print(i)
            print()


if __name__ == '__main__':
    task_multiprocessing()
    task_multithread()
    import sys
    print(sys.path)
    func = task_multithread
    print(func.__class__)


nums = [12,2,34]

你可能感兴趣的:(多线程编程、 Linux 编程)