Python 基础语法 Hello, World!

Python 基础语法

import concurrent.futures
import threading
import time


def switch_case(argument):
    switch = {
        1: "这是第一种情况",
        2: "这是第二种情况",
        3: "这是第三种情况"
    }
    return switch.get(argument, "这是默认情况")


def my_function():
    print("开始阻塞", end="\n")
    # 阻塞一秒
    time.sleep(1)
    print("阻塞结束", end="\n")



def main():
    print("Hello, World!")
    """
    for
    """
    for i in range(1):
        print("Hello, World!", i)
    str_tmp = "str_tmp"
    print(str_tmp)
    x = 10
    """
    if
    """
    if x > 5:
        print("x 大于 5")
    else:
        print("x 不大于 5")

    str1 = "hello"
    str2 = "hello"
    """
    switch 结果
    """
    print("switch 结果:" + switch_case(2))
    if str1 == str2:
        print("两个字符串相等")
    else:
        print("两个字符串不相等")

    # 创建子线程

    my_thread = threading.Thread(target=my_function)

    # 启动子线程
    my_thread.start()

    # 等待子线程执行完毕
    my_thread.join()
    print("子线程执行完毕")

    # 创建线程池
    with concurrent.futures.ThreadPoolExecutor() as executor:
        # 提交任务给线程池
        future1 = executor.submit(my_function)
        future2 = executor.submit(my_function)
    # 等待子线程结束
    executor.shutdown(wait=True)
    # 等待子线程结束
    # for future in concurrent.futures.as_completed([future1, future2]):
    #     # 处理已完成的任务
    #     print("处理已完成的任务")
    #     pass

    print("All tasks are done")


if __name__ == "__main__":
    main()

你可能感兴趣的:(Python,python)