B
B
Bergis2019-07-22 20:56:46
Python
Bergis, 2019-07-22 20:56:46

How to implement several processes at once in 1 program?

I wrote the code, but I want the program to accept an infinite number of new api and work with them. How can I do that ? This program only works with 1 new api.

import vk
import pyowm
from datetime import datetime, timedelta
import vk_api
from time import sleep 
import os
import sys
import random


session1 = vk.AuthSession(access_token='')
vk_api = vk.API(session1, v=5.62)
now1 = str(datetime.strftime(datetime.now(), "%H:%M"))
messages_text  = ['Токен', 'токен','кpecт']

def stat(tok):
  owm = pyowm.OWM('6d00d1d4e704068d70191bad2673e0cc', language = "ru")
  observation = owm.weather_at_place("Москва")
  session2 = vk.AuthSession(access_token=tok)
  vk_api1 = vk.API(session2, v=5.62)
  now12 = str(datetime.strftime(datetime.now(), "%H:%M"))
  while True:
    w = observation.get_weather()
    temp = w.get_temperature('celsius')["temp"]
    vk_api1.account.setOnline()
    now = str(datetime.strftime(datetime.now(), "%H:%M"))
    if now!=now12:
      vk_api1.status.set(text= "Время в Москве : " + now+ ", " + " Температура в Москве : " + str(temp) + " градусов" + ", " + " погода : " + w.get_detailed_status())
      now1=now
  
    sleep(30)

while True:
  messages = vk_api.messages.getConversations(offset = 0, count = 20, filter = "unread") 
  if messages["count"] >= 1: 		                                           
    id = messages["items"][0]["last_message"]["from_id"] 
    body = messages["items"][0]["last_message"]["text"] 
    message = body.lower()
    if message in list(messages_text):
      if message  == "Токен" or message == "токен" or message == "тoкeн": 
        a = random.randint(1, 999999999)
        vk_api.messages.send(peer_id = id, message= "Кидай свой токен :", random_id = a)
        while True:
          messages = vk_api.messages.getConversations(offset = 0, count = 20, filter = "unread")
          if messages["count"] >= 1:  
            id1 = messages["items"][0]["last_message"]["from_id"] 
            if id == id1:
              body = messages["items"][0]["last_message"]["text"] 
              message = body.lower()
              tok = str(message)
              print(tok)
              stat(tok) 
            else:		
   							sleep(15)

    else:		
   			sleep(15)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
l7_7l, 2019-07-22
@Bergis

In this case, you need to create a new thread.
To work with threads from the threading module, we import the Thread class. At the beginning of the code, we write: After that, the Thread () function will be available to us - it is easy to create threads with it. The syntax is:
Source: https://geekbrains.ru/posts/python_threading_part1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question