0

I would like to make a python code that will:

  1. Read data from api and refresh every 10 minutеs.

  2. Display this data on LCD display in two sheets which change continuously every 5 seconds

I do not know how to make one part of the code run independently of the other. In my case.. - BLOCK1 run every 300s - BLOCK2 run nonstop

Here is my Python code... of course, it's not working and is not finished yet. Thank you for your help!

from urllib import urlopen
import I2C_LCD_driver1
import json
import time
mylcd = I2C_LCD_driver1.lcd()

while True:
    # BLOCK 1 - start every 600s
    CNV7 = urlopen('https://www.coincalculators.io/api/allcoins.aspx?hashrate=12200&power=1400&powercost=0.15&difficultytime=0&algorithm=CryptoNightV7').read()
    dataCNV7= json.loads(CNV7)  
    coinCNV7 = dataCNV7[0]["name"] 
    algoCNV7 = dataCNV7[0]["algorithm"] 
    dayUSDCNV7 = dataCNV7[0]["profitInDayUSD"]
    print ("Algoritm:"),algoCNV7
    print ("Coin:"),coinCNV7
    dayUSDCNV7 = float(dayUSDCNV7)
    dayEUCNV7 = dayUSDCNV7*0.88
    print("%.2f" % dayEUCNV7),("Eu/dan")
    time.sleep(600) # Read API every 10minuts

    # BLOCK 2 - must run non-stop
    if dayEUCNV7 > 3:
        while True:
            print("RELEY ON")
            mylcd.lcd_clear()
            mylcd.lcd_display_string("RELEY - ON",1,0)
            mylcd.lcd_display_string("Profit:",2,0)
            mylcd.lcd_display_string(str(dayEUCNV7),3,2)
            print ("LCD page 1")
            time.sleep(2)

            mylcd.lcd_clear()
            mylcd.lcd_display_string("RELEY- ON",1,0)
            mylcd.lcd_display_string(str(coinCNV7),3,2)
            print ("LCD page 2")
            time.sleep(2)

    else:
        while True:
            print("RELEY OFF")
            mylcd.lcd_clear()
            mylcd.lcd_display_string("RELEY - OFF",1,0)
            mylcd.lcd_display_string("Profit:",2,0)
            mylcd.lcd_display_string(str(dayEUCNV7),3,2)
            print ("LCD page 1")
            time.sleep(2)

            mylcd.lcd_clear()
            mylcd.lcd_display_string("RELEY- OFF",1,0)
            mylcd.lcd_display_string(str(coinCNV7),3,2)
            print ("LCD page 2")
            time.sleep(2)
dmitryro
  • 3,463
  • 2
  • 20
  • 28
McDam
  • 25
  • 5

1 Answers1

0

Function 1 executing block 1 will run every 10 seconds and function 2 considering some execution time will execute continuously.

import threading
import time
def func1(f_stop):
    print(1)
    # BLOCK 1 ...
    if not f_stop.is_set():
        # call func1() again in 10 seconds
        threading.Timer(10, func1, [f_stop]).start()

def func2():
    print(2)
    # BLOCK 2 ...
    time.sleep(2) # For demonstration, remove this line
    threading.Timer(0, func2).start()

# start calling func1 now and every 10 sec thereafter
f_stop = threading.Event()
func1(f_stop)
# Call func2 which will run forever with a delay of 2 sec
func2()
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
  • Run once.. i need loop all code.... .. if i add While True: have error : threading.Thread(target=func2).start() error: can't start new thread – McDam Nov 03 '18 at 16:57
  • There was an interesting bug in the code. Have updated the code. – Vishnudev Krishnadas Nov 03 '18 at 17:08
  • while the method works for sure i would recommend that you go with multi-processing instead of multi-threading , multi-threading in python is not actually multi-threading ( See GIL) . – Niteya Shah Nov 03 '18 at 17:25
  • @NiteyaShah You say not to use python for this case? – Vishnudev Krishnadas Nov 03 '18 at 17:29
  • @McDam declare `dayEUCNV7` globally or pass as args to the function. Variable in one function is not accessible to other, – Vishnudev Krishnadas Nov 03 '18 at 17:34
  • @Vishnudev i must all declare ??? gloabal dayEUCNV7 ; dayEUCNV7 = 0 ....... This is lot of typing... my code will be very long ?! coinCNV7 algoCNV7 dayUSDCNV7 and then another 10 API for dayEUCNV8 ?? – McDam Nov 03 '18 at 17:44
  • Sorry to say this but if you have variable dependency then I suggest using a shared [Queue](https://docs.python.org/2/library/queue.html). Its better you start learning from scratch of multi-threading in python. This is not an easy problem. I assume that you haven't coded using threads in python. – Vishnudev Krishnadas Nov 03 '18 at 17:48
  • my code will be very long and worst in the world, but it will work. Dont forget... this is my hobby. I make some code in python, but i have same problem....Look in some files for data every 30minuts, then all the time print on LCD display.... Problem is with declare all value ......Thankyou – McDam Nov 03 '18 at 17:50
  • You know the consequences of using nested while True loops right, that too without a break statement. – Vishnudev Krishnadas Nov 03 '18 at 17:53
  • If i use while true: dont need to declare? correct ? When i use function must declare ? – McDam Nov 03 '18 at 17:57
  • @Vishnudev see multi-processing for python , i would recommend python for rasberry pi for learning it but the more advanced projects that i have done have often have used C/C++ because python starts getting slow and speed is a major concern for rpi(I had to overclock my rpi), and while the newer version has gotten quite fast I still dont think a rpi can handle many tasks(real time video was what i working with) – Niteya Shah Nov 03 '18 at 17:58
  • I agree with you @NiteyaShah. It gets slow with heavy processing such as real-time video. Even I have worked on that. But calling an API shouldn't be an issue. – Vishnudev Krishnadas Nov 03 '18 at 18:14
  • @McDam Once it enters a while loop say the while loop inside if statement, it will stay there forever. It wouldn't run the outer while loop. – Vishnudev Krishnadas Nov 03 '18 at 18:17
  • Thanks all for help........... @Vishnudev do you have any advice on how to measure how long the relay is already ON, in the IF function? If i use start = time.time(), measure only time of loop, not SUM time ON – McDam Nov 03 '18 at 18:23
  • Exactly why I asked you to be careful with implementing using while loop. There's no way because you never break inside RELAY-ON – Vishnudev Krishnadas Nov 03 '18 at 18:27
  • and what is secong way ? – McDam Nov 03 '18 at 18:30
  • Use Threading with Shared Queues. Use global variable for non dynamic variable dependency – Vishnudev Krishnadas Nov 03 '18 at 18:46
  • @Vishnudev i am not saying this wont work , my point here was that multi-threading in python is kind fake ( I really cant think of a better word so sorry in advance) . While for small tasks we can use and achieve a pseudo - multi threaded environment , in reality its just one thread that schedules many tasks and getting this rightis very important early on, multi-processing on the other hand is quite effective and will actually bring about an improvement in performance , which is why i recommend it people trying to multi-thread in python . – Niteya Shah Nov 03 '18 at 18:56
  • This comments section is going out of scope now. This is my last message. – Vishnudev Krishnadas Nov 03 '18 at 19:06