プログラムの実装 コードの実装を一時停止して続行する

要件: コンソールに 0 を入力して Enter キーを押すとプログラムが一時停止され、1 を入力するとプログラムが続行されます。

ユーザーが 0 を入力した後も 0 を入力し続けると、1 を入力した後も一時停止され、プログラムは変更を送信しません。

def input_thread():   # 直接把user参数删掉  哈哈  你传参干嘛 直接global user不就行了 不就解决了动态绑定的需求了哈哈
    global a,user
    # with condition:

    while True:
        last_a = a
        print(user)
        a = input("暂停(0)/继续(1):")
        # while last_a == a or not (a=='1' or a=='0'):
        #     a = input("暂停(0)/继续(1):")
        #     print("last_a:", last_a, "a:", a)
        if a == '1' and last_a !='1':
            user.getcond().acquire()  # 对条件变量进行 acquire 操作
            user.getcond().notify()
            user.getcond().release()  # 对条件变量进行 release 操作

新しいスレッドを作成してユーザー入力を継続的に取得します

inputThread = threading.Thread(target=input_thread)
inputThread.setDaemon(True)  # 将 stop_continue 线程设置为守护线程
inputThread.start()

完全なコード

import random
import subprocess
import time
from random import choice
import pyautogui
import pyperclip
import threading
import keyboard
import pyperclip

pyautogui.FAILSAFE = False
import win32gui
import win32con

# import 运行界面设为左下方位置


class User01(threading.Thread):
    # runy, x, ad_count = 14, ad_download_count = 10, uninstall = 5, rank = 1

    def __init__(self, cond, runy, x, ad_count=14, ad_download_count=10, uninstall=5, rank=1, ad_fail=0, left_user = '1邹飞',right_user = '2汪燕  李胜'):
        super(User01, self).__init__()

        self.cond = cond
        self.x = x-2400
        self.rank = rank
        self.runy = runy
        self.ad_count = ad_count
        self.ad_download_count = ad_download_count
        self.uninstall = uninstall
        self.ad_fail = 0
        self.current_ad_count = 0
        self.current_ad_download_count = 0
        self.video_in = 0  # 进入了的视频个数
        self.not_in_video = 0
        self.left_user = left_user
        self.right_user = right_user

        # self.pause_event = threading.Event()
        # self.pause_event.set()
    # def pause(self):
    #     """暂停线程"""
    #     # self.pause_event.clear()
    #     self.cond.clear()
    # def resume(self):
    #     """恢复线程"""
    #     # self.pause_event.set()
    #     self.cond.set()
    def getcond(self):
        return self.cond
    def run(self):  # rank是第几个运行的意思

        pyautogui.FAILSAFE = False  # 防故障安全措施
        release_list = []  # 释放键位列表,避免强行终止时有键位没有松开
        start_width, start_height = pyautogui.size().width, pyautogui.size().height  # 获取启动时屏幕分辨率

        def main():
            self.cond.acquire()
            global rank1, rank2, videos, position1, position2
            selfvideos = []
            if self.rank == 2:
                first5_videos = videos[:5]
                rest_videos = videos[5:]
                rest_videos.extend(first5_videos)
                videos = rest_videos
                selfvideos = videos
            else:
                selfvideos = videos
            i = 0
            while True:
                if i >= 3 :
                    rank1 = 0
                    position1 = 0
                    self.cond.release()
                    print(str(self.x) + "运行结束")
                    break
                print("i="+str(i))
                for even_index in range(0, len(selfvideos), 2):  # even:偶数
                    two_videos = selfvideos[even_index:even_index + 2]
                    for video in two_videos:
                        if i >= 10:
                            rank1 = 0
                            position1 = 0
                            self.cond.release()
                            print(str(self.x) + "运行结束")
                            return
                        i += 1  # 每次循环都搞20多个视频噢   搞两次即40多次!
                        time.sleep(2)
                        print(video+",",end='')
                        # with self.cond:
                        if a == '0':
                            print("a=",a)
                            # self.pause()
                            self.cond.wait()
                        # if a == '1':
                        #     print("a=",a)
                        #     self.cond.notify()

        main()
    def __str__(self):
        return self.left_user+" "+self.right_user

new_videos = [
    # paprika:
    'BV1c841137Zn',  # 免费下载全网所有音乐 详细教程
    'BV1jg4y1p7sW',  # listen1下载全网音乐 详细教程
    # firefox:
    # 'BV1n84y157HG',  # 下载全网所有vip音乐 实现音乐自由!
    'BV1xM411e7cG',  # listen 1下载全网所有vip音乐教程
    'BV19o4y1e7jR'  # BV19o4y1e7jR
]
old_amazing_videos = [
    # paprika:
    'BV1oe4y1Z7MF',  # 你从什么时候开始堕落了? 别让等待成为遗憾!!!!!!!!!!!!!!!!
    'BV1Ld4y1G7Zv',  # 动画《大王饶命》:我什么都不要,我只想要我们这个家!!!!!!!!!!!!!!
    # firefox:
    'BV1bG4y1m7CF',  # “年 少 不 懂 姐 姐 好”!!!!!!!!!!!!!
    'BV1F8411E7Us',  # 《卧槽?这真的才16岁》日常迷惑行为大赏!!!!!!!!!!!!!!!
    'BV1q8411b7QT',  # Howl's Moving Castle!!!!!!!!!!!!!!!!

    'BV1SP411n7wW',  # “监 控 下 的 社 死”!!!!!!!!!!!!
    'BV1fe4y1h75p',  # 东京奥运会高燃夺冠瞬间每一个都让人热血沸腾!!!!!!!!!!!!!!!
    'BV1od4y1K7M9',  # 爬虫进阶 破解JS逆向和AES加密!!!!!!!!!!!!!
    'BV1oU4y167RF',  # 【动画剪辑】声之形:有一瞬间觉得自己真的走错了好多路。!!!!!!!!
    'BV1fv4y1X7W4',  # 三体 交的女友 出门就领了盒饭!!!!!!!!!!!!!!!!!!

]
old_good_videos = [
    # paprika:
    'BV13B4y197Y8',  # 4K暖暖房间 动态壁纸高清高帧率
    'BV1eR4y1g758',  # 英语演讲模仿训练第三弹|呼吁大家好好学习
    'BV16F411v7un',  # 英语演讲模仿训练|呼吁大家学习计算机科学

    # firefox:
    'BV1pT411M7Gn',  # 魔法少女小圆AMV,上帝已死,在绝望的深渊中
    'BV13W4y1i7og',  # “真正走掉那次,关门声最小。
    'BV1ie411u7bP',  # “ 或许只有遗憾才能让人念念不忘 ”
]
videos = []
videos.extend(new_videos)
videos.extend(old_amazing_videos)
videos.extend(old_good_videos)


def update_video_urls(videos):

    datas = open(r"C:\Users\LENOVO\Desktop\bilibili自动互粉脚本\最新视频urls02.txt",encoding="utf-8")
    contents = datas.readlines()
    videos_urls = []
    for line in contents:
        word = ''
        for i in line:
            if i !=" ":
                word = word+i
                # print(i)
            else:
                break
        videos_urls.append(word)

    updated_list = videos_urls
    updated_list.extend(videos)
    return updated_list

videos = update_video_urls(videos)
flag = 1
con = threading.Condition()
# 同时运行第1、2个
user01 = User01(con, runy=341, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='1邹飞',right_user='2汪燕  李胜')  # 1邹飞

rank1 = 1
rank2 = 1
count1 = 5
count2 = 15
# user02.start()
# user01.start()

position1 = 1  # 有人
position2 = 1  # 有人

switch_ = 0
def shutdown_two_phones():
    # 第一:
    pyautogui.click((419-2400)/1.25, 12/1.25+5, duration=0.2)  # 点击×!!!!
    time.sleep(1)
    # 第二:
    pyautogui.click((419 + 480-2400)/1.25, 12/1.25+5, duration=0.2)  # 点击×!!!!
    time.sleep(1)

    # 第一:
    pyautogui.click((419-2400)/1.25, 12/1.25+5, duration=0.2)  # 点击×!!!!
    time.sleep(1)
    # 第二:
    pyautogui.click((419 + 480-2400)/1.25, 12/1.25+5, duration=0.2)  # 点击×!!!!
    time.sleep(1)


# from 多账号依次三连 import crawl_run


users = open(r'C:\Users\LENOVO\Desktop\bilibili自动互粉脚本\userOrder.txt', encoding="utf-8")
users_list = users.read().split(',')
# userDatas = eval(users.read())
print("users_list:", users_list)
# print(users.read())
users.close()
users_list_for_change = users_list
print(users_list_for_change)


def update_userOrder():
    global users_list_for_change,users_list

    # 将修改后的列表保存到文件
    users_list_for_change = users_list_for_change[1:]
    users_list_for_change.append(users_list[0])
    users_list = users_list_for_change
    # print(users_list_for_change)
    changed_list_str = ','.join(users_list_for_change)
    # print(changed_list_str)
    # 打开文件,并写入该字符串
    f = open(r'C:\Users\LENOVO\Desktop\bilibili自动互粉脚本\userOrder.txt', 'w', encoding="utf-8")
    f.write(changed_list_str)
    f.close()
last_a = '1'
a = '1'
user = ''
def input_thread():   # 直接把user参数删掉  哈哈  你传参干嘛 直接global user不就行了 不就解决了动态绑定的需求了哈哈
    global a,user
    # with condition:

    while True:
        last_a = a
        print(user)
        a = input("暂停(0)/继续(1):")
        # while last_a == a or not (a=='1' or a=='0'):
        #     a = input("暂停(0)/继续(1):")
        #     print("last_a:", last_a, "a:", a)
        if a == '1' and last_a !='1':
            user.getcond().acquire()  # 对条件变量进行 acquire 操作
            user.getcond().notify()
            user.getcond().release()  # 对条件变量进行 release 操作
    # if a == '0':
    #     while last_a == a:
    #         a = input("暂停(0)/继续(1):")
def whole_run():

    global position1,a,user
    position1 = 1  # 有人
    to_shutdown_flag = True  # 如果第一个是-1则第一个结束后shutdown
    switch_ = -1
    switch_ = int(users_list_for_change[0])
    if switch_ == -1:
        # update_userOrder()
        user01.start()
        user = user01
        switch_ += 1
    else:
        to_shutdown_flag = False  # 第一次不要shutdown  因为第一次就是我刚进来
        position1 = 0
    # if not user=='':
    # condition = threading.Condition()
    # t = threading.Thread(target=input_thread, args=(user,))
    # t.daemon = True  # 将子线程设置为守护线程
    # t.start()

    input_flag_stop = 1
    input_flag_resume = 1
    while True:
        # a = input("暂停(0)/继续(1):")

        # time.sleep(5)
        try:
            if position1 == 0:
                # 首先把两个模拟器关了
                time.sleep(1)
                # if to_shutdown_flag:
                    # shutdown_two_phones()
                to_shutdown_flag = True  # 我们只是看第第一次是否要shutdown 如果第一个是-1就本来就是True

                if switch_ == 0:
                    update_userOrder()
                    switch_ += 1
                    print("启动第3、4个")
                    user03 = User01(con, runy=441, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='3谢金岚',right_user='415270732168')  # 3 李胜
                    user03.start()
                    user = user03
                elif switch_ == 1:
                    update_userOrder()
                    switch_ += 1
                    print("启动第5、6个")
                    user05 = User01(con, runy=541, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='5邹飞',right_user='6李胜')  # 515270732168
                    user05.start()
                    user = user05

                elif switch_ == 2:
                    update_userOrder()
                    switch_ += 1
                    print("启动第7、8个")
                    user07 = User01(con, runy=641, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='7李胜小号',right_user='813979732881')  # 7李胜
                    user07.start()
                    user = user07
                elif switch_ == 3:
                    update_userOrder()
                    switch_ += 1
                    print("启动第1、2个")
                    user09 = User01(con, runy=341, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='1邹飞',right_user='2汪燕  李胜')  # 1邹飞
                    user09.start()
                    user = user09
                elif switch_ == 4:
                    update_userOrder()
                    switch_ += 1
                    print("启动第3、4个")
                    user11 = User01(con, runy=441, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='3谢金岚',right_user='415270732168')  # 3 李胜
                    user11.start()
                    user = user11
                elif switch_ == 5:
                    update_userOrder()
                    switch_ += 1
                    print("启动第5、6个")
                    user13 = User01(con, runy=541, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='5邹飞',right_user='6李胜')  # 515270732168
                    user13.start()
                    user = user13
                elif switch_ == 6:
                    update_userOrder()
                    switch_ += 1
                    print("启动第7、8个")
                    user15 = User01(con, runy=641, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='7李胜小号',right_user='813979732881')  # 7李胜
                    user15.start()
                    user = user15



                elif switch_ == 7:
                    update_userOrder()
                    switch_ += 1
                    print("启动第1、2个")
                    user17 = User01(con, runy=341, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='1邹飞',right_user='2汪燕  李胜')  # 1邹飞
                    user17.start()
                    user = user17
                elif switch_ == 8:
                    update_userOrder()
                    switch_ += 1
                    print("启动第3、4个")
                    user19 = User01(con, runy=441, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='3谢金岚',right_user='415270732168')  # 3 李胜
                    user19.start()
                    user = user19
                elif switch_ == 9:
                    update_userOrder()
                    switch_ += 1
                    print("启动第5、6个")
                    user21 = User01(con, runy=541, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='5邹飞',right_user='6李胜')  # 515270732168
                    user21.start()
                    user = user21
                elif switch_ == 10:
                    update_userOrder()
                    switch_ += 1
                    print("启动第7、8个")
                    user23 = User01(con, runy=641, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='7李胜小号',right_user='813979732881')  # 7李胜
                    user23.start()
                    user = user23
                elif switch_ == 11:
                    update_userOrder()
                    switch_ += 1
                    print("启动第1、2个")
                    user101 = User01(con, runy=341, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='1邹飞',right_user='2汪燕  李胜')  # 1邹飞
                    user101.start()
                    user = user101
                elif switch_ == 12:
                    update_userOrder()
                    switch_ += 1
                    print("启动第3、4个")
                    user103 = User01(con, runy=441, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='3谢金岚',right_user='415270732168')  # 3 李胜
                    user103.start()
                    user = user103
                elif switch_ == 13:
                    update_userOrder()
                    switch_ += 1
                    print("启动第5、6个")
                    user105 = User01(con, runy=541, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='5邹飞',right_user='6李胜')  # 515270732168
                    user105.start()
                    user = user105
                elif switch_ == 14:
                    update_userOrder()
                    switch_ += 1
                    print("启动第7、8个")
                    user107 = User01(con, runy=641, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='7李胜小号',right_user='813979732881')  # 7李胜
                    user107.start()
                    user = user107
                elif switch_ == 15:
                    update_userOrder()
                    switch_ += 1
                    print("启动第1、2个")
                    user201 = User01(con, runy=341, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='1邹飞',right_user='2汪燕  李胜')  # 1邹飞
                    user201.start()
                    user = user201
                elif switch_ == 16:
                    update_userOrder()
                    switch_ += 1
                    print("启动第3、4个")
                    user203 = User01(con, runy=441, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='3谢金岚',right_user='415270732168')  # 3 李胜
                    user203.start()
                    user = user203
                elif switch_ == 17:
                    update_userOrder()
                    switch_ += 1
                    print("启动第5、6个")
                    user205 = User01(con, runy=541, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='5邹飞',right_user='6李胜')  # 515270732168
                    user205.start()
                    user = user205
                elif switch_ == 18:
                    update_userOrder()
                    switch_ += 1
                    print("启动第7、8个")
                    user207 = User01(con, runy=641, x=0, ad_count=4, ad_download_count=1, uninstall=0, rank=1,left_user='7李胜小号',right_user='813979732881')  # 7李胜
                    user207.start()
                    user = user207
                elif switch_ == 19:
                    input("运行已结束,请指示: ")
                    break
                # elif switch_ == 15:
                #     switch_ += 1
                #     print("启动多账号三连")
                #     crawl_run()   # 多账号三连
                #     break

                # elif switch_ == 15:
                #     switch_ += 1
                #     print("启动第1个")
                #     user17.start()
                # elif switch_ == 16:
                #     switch_ += 1
                #     print("启动第2个")
                #     user18.start()
                # elif switch_ == 17:
                #     switch_ += 1
                #     print("启动第3个")
                #     user19.start()
                # elif switch_ == 18:
                #     switch_ += 1
                #     print("启动第4个")
                #     user20.start()
                # elif switch_ == 19:
                #     switch_ += 1
                #     print("启动第5个")
                #     user21.start()
                # elif switch_ == 20:
                #     switch_ += 1
                #     print("启动第6个")
                #     user22.start()
                # elif switch_ == 21:
                #     switch_ += 1
                #     print("启动第7个")
                #     user23.start()
                # elif switch_ == 22:
                #     switch_ += 1
                #     print("启动第8个")
                #     user24.start()
                #     break

                # elif switch_ == 23:
                #     switch_ += 1
                #     print("启动多账号三连")
                #     crawl_run()  # 多账号三连
                #     break
                position1 = 1  # 设置为有人
            time.sleep(5)
        except Exception as e:
            print("错误原因:", e)
            switch_ += 1
            time.sleep(5)
            # shutdown_two_phones()
            # # 首先把两个模拟器关了
            # # 第一:
            # pyautogui.click(419, 12, duration=0.2)  # 点击×!!!!
            # time.sleep(1)
            # pyautogui.click(300, 537, duration=0.2)  # 点击×!!!!
            # time.sleep(0.5)
            # # 第二:010
            # pyautogui.click(419 + 480, 12, duration=0.2)  # 点击×!!!!
            # time.sleep(1)
            # pyautogui.click(300 + 480, 537, duration=0.2)  # 点击×!!!!
            # time.sleep(0.5)
            # # 再次关闭  防止关闭失败
            # # 第一:
            # pyautogui.click(419, 12, duration=0.2)  # 点击×!!!!
            # time.sleep(1)
            # pyautogui.click(300, 537, duration=0.2)  # 点击×!!!!
            # time.sleep(0.5)
            # # 第二:
            # pyautogui.click(419 + 480, 12, duration=0.2)  # 点击×!!!!
            # time.sleep(1)
            # pyautogui.click(300 + 480, 537, duration=0.2)  # 点击×!!!!
            # time.sleep(0.5)
            position1 = 0
        # try:
        #     if a == '1':
        #         # user01.cond.notify()
        #         if input_flag_stop:
        #             # a = input("暂停(0)/继续(1):")
        #             condition = threading.Condition()
        #             t = threading.Thread(target=input_thread, args=(user,))
        #             t.daemon = True  # 将子线程设置为守护线程
        #             t.start()
        #             input_flag_stop = 0
        #             input_flag_resume = 1  # 开放0中输入的权限
        #         # else:
        #
        #     if a == '0':
        #         if input_flag_resume:
        #             # a = input("暂停(0)/继续(1):")
        #             condition = threading.Condition()
        #             t = threading.Thread(target=input_thread, args=(user,))
        #             t.daemon = True  # 将子线程设置为守护线程
        #             t.start()
        #
        #             input_flag_resume = 0
        #             input_flag_stop = 1  # 开放1中输入的权限
        # except Exception as e:
        #     print("暂停或继续失败!可能是因为模拟器正在关闭中...",e)

# run_4(5,6,7,8)

# import 运行界面设为左下方位置
import keyboard
import threading
import os
def space_stop_continue():
    # 按下键盘
    def Press(x):
        global a
        if x.event_type == 'down':
            # print(x.name)
            # threading.Thread(target=).start()
            if x.name == 'space':
                a = '1' if a == '0' else '0'
                # a = a =='1'?'1':'0'
                print(a)
            elif x.name == 'f6':
                a = '0'
                print('0')
            elif x.name == 'f7':
                a = '1'
                print('1')
                try:
                    user.getcond().acquire()  # 对条件变量进行 acquire 操作
                    user.getcond().notify()
                    user.getcond().release()  # 对条件变量进行 release 操作
                except:
                    print("user不存在,请重试......")
    keyboard.hook(Press)
    keyboard.wait()
    os.system('pause') #暂停程序
stop_continue = threading.Thread(target=space_stop_continue)
stop_continue.setDaemon(True)  # 将 stop_continue 线程设置为守护线程
stop_continue.start()

inputThread = threading.Thread(target=input_thread)
inputThread.setDaemon(True)  # 将 stop_continue 线程设置为守护线程
inputThread.start()

whole_run()
input("运行已结束,请指示: ")
# user08.start()
# user07.start()

おすすめ

転載: blog.csdn.net/conquer_galaxy/article/details/130454511