Программа не будет спать - PullRequest
0 голосов
/ 24 января 2020

Я пытаюсь сделать так, чтобы мой код вызывал user3 () в течение 10 секунд, затем спал в течение 30 секунд постоянно (навсегда), но он никогда не спит. Я поиграл с кодом и думаю, что он как-то связан с порядком, в котором я отформатировал вещи в user3 (), но я не уверен, где.

gramcrawler.py

import instaloader
import time
import os
import pathlib
import requests
import threading
from multiprocessing import Process
L = instaloader.Instaloader(download_comments=False, download_videos=False, download_video_thumbnails=False,
                            save_metadata=False)
data_path = pathlib.Path(os.getcwd())/"data"

def user3(L, data_path):
        #login information
        L.login("********", "********")  # (login)
        file2= open("followers3.txt","r")
        data = ""
        lines = file2.readlines()
        line_count=0

        for line in lines:
            data = line.strip()
            profile = instaloader.Profile.from_username(L.context, data)
            number = profile.followees

            for followee in profile.get_followees():
                    file2 = open("followers3.txt", "a+")
                    allusers = open("allusers.txt", "a+")
                    #username = followees[followee]
                    username = followee.username
                    a = followee.userid
                    file2.write("\n"+ str(username))
                    line_count += 1
                    line_count2+=1
                    allusers.write("\n" + str(username))

                    print(username + " User3")
            if line_count==number:
                f = open('followers3.txt', 'r')
                lines = f.readlines()
                f.close()

                f = open('followers3.txt','w' )
                f.write('\n'.join(lines[1:]))
                f.close()


        #close the text file
            file2.close()
def user3timed():
    while True:
        s = time.time()
        while time.time() < s + 10:
            user3(L,data_path)
        time.sleep(60)

if __name__=='__main__':
    p3 = Process(target=user3timed())
    p3.start()
    p3.join()

follow3.txt

liluzivert

1 Ответ

0 голосов
/ 24 января 2020

Строка с «Пока» в нем не имеет смысла. Это делает бесконечным, пока l oop. Это все равно, что спросить: «Текущее время раньше, чем будет через 10 секунд?».

У меня исправлено Это немного больше для правильной работы.

import instaloader
import time
import os
import pathlib
import requests
import threading
from multiprocessing import Process
L = instaloader.Instaloader(download_comments=False, download_videos=False, download_video_thumbnails=False,
                            save_metadata=False)
data_path = pathlib.Path(os.getcwd())/"data"

def user3(L, data_path):
        #login information
        s = time.time() #checks the time the function starts
        L.login("********", "********")  # (login)
        file2= open("followers3.txt","r")
        data = ""
        lines = file2.readlines()
        line_count=0

        for line in lines:
            data = line.strip()
            profile = instaloader.Profile.from_username(L.context, data)
            number = profile.followees

            for followee in profile.get_followees():
                    file2 = open("followers3.txt", "a+")
                    allusers = open("allusers.txt", "a+")
                    #username = followees[followee]
                    username = followee.username
                    a = followee.userid
                    file2.write("\n"+ str(username))
                    line_count += 1
                    line_count2+=1
                    allusers.write("\n" + str(username))

                    print(username + " User3")
            if line_count==number:
                f = open('followers3.txt', 'r')
                lines = f.readlines()
                f.close()

                f = open('followers3.txt','w' )
                f.write('\n'.join(lines[1:]))
                f.close()


        #close the text file
            file2.close()
        while(s+10<time.time()): #compare time to the start of function
            time.sleep(0.1)
def user3timed():
    while True:
        user3(L,data_path)
        time.sleep(60)

if __name__=='__main__':
    p3 = Process(target=user3timed())
    p3.start()
    p3.join()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...