0

I am new to threading, I'm able to read the data from the database using (def value) function and passing that data to (def main). I've created an infinite loop so that whenever new value is added to database, the (def value) function can read it and pass the new data accordingly, which will be passed to another device. But new data is not acquired automatically, I've to start the program again in order to load the new program and pass it to different device. I'm confused why I'm not getting new value while the loop is still running.

Here is the data format:

data = {'data': 'xyz', 'code': '<:c:605445>  **[Code](https://traindata/35.6547,56475', 'time': '2021-12-30T09:56:53.547', 'value': 'True', 'stats': '96/23', 'dupe_id': 'S<:c-74.18'}

Here is the code:

def value(id): 
        headers = { 'authorization': 'xyz'} #authorization token here
        
        r = requests.get(f'https://traindata.com/api/v9/{id}', headers=headers) #website link along with variable passed here
        jsonn = json.loads(r.text)  #read the values 
        i = 0
        d = {}
        while i < len(jsonn):
              data = jsonn[i]["content].split("")[1]            #splitting information which is needed 
              d[str(i)] = {}
              d[str(i)]["data"] = data
              d[str(i)]["code"] = code
              i+= 1 
        return d
X = value('987654')                   #passing the id which gives data from specific file

def main(cds):
            data = X                    #passing value which was outside the loop
            for s in data.values():
                  print(s)
            while data != "stop":                      # unless I press stop the program should run 
                  if data == "stop":                   #when I press stop the program stops
                       os.system("disconnect")
                  else:
                       cds = d['code'].split('/')[-1].split(',') 
 #splits value which is needed to be passed onto another device
                  return cds
m = main(cds) 
if __name__ == "__main__": 
     t1 = Thread(target=value, args=(987654, ))           #thread 1
     t2 = Thread(target=main, args=(m, ))                 #thread 2 
     t1.setDaemon(True)  
     t2.setDaemon(True)
    
     t1.start()
     t2.start()   
     t1.join()      
     t2.join()
     while True:
        pass

The output I'm getting is this:

Number of active threads: 1
Number of active threads: 1
{'data': 'xyz', 'code': '<:c:605445>  **[Code](https://traindata/35.6547,56475', 'time': '2021-12-30T09:56:53.547', 'value': 'True', 'stats': '95/23', 'dupe_id': 'S<:c-74.18'}
35.6547,56475
Number of active threads ae: 2
Number of active threads: 3
{'data': 'xyz', 'code': '<:c:605445>  **[Code](https://traindata/35.6547,56475', 'time': '2021-12-30T09:56:53.547', 'value': 'True', 'stats': '95/23', 'dupe_id': 'S<:c-74.18'} #same data is printed as previous one 

Same data is printed even after thread is running in infinite loop. I want something like this:

Number of active threads: 1
Number of active threads: 1
{'data': 'xyz', 'code': '<:c:605445>  **[Code](https://traindata/35.6547,56475', 'time': '2021-12-30T09:56:53.547', 'value': 'True', 'stats': '95/23', 'dupe_id': 'S<:c-74.18'}
35.6547,56475
Number of active threads ae: 2
Number of active threads: 3
{'data': 'xyz', 'code': '<:c:605455>  **[Code](https://traindata/42.6247,28.47023', 'time': '2021-12-30T09:59:57.758', 'value': 'True', 'stats': '90/110', 'dupe_id': 'S<:c-74.18'} #different data should be printed (having different time stamp, and code)
42.6247,28.47023
Sukrut Shishupal
  • 138
  • 1
  • 12
  • what is `cds`? Its undefined for now. There is that `do something` which basically is the value of `X`. If not possible to make that bit public, can you give a sample value of `X`? – vish4071 Jan 12 '22 at 11:00
  • Also, there is that line in `main`, which is `while data != "stop"`, but `data` is a dictionary, no? – vish4071 Jan 12 '22 at 11:02
  • There are so many issues with this code...anyway, why are you sending `cds` as argument in `main` when you are not using it? Why is your function named `main` when it is not the driver function of the script? – vish4071 Jan 12 '22 at 11:05
  • Since you are using unconditional `return` in both `main` and `value` functions, `while True` is totally redundant. – vish4071 Jan 12 '22 at 11:06
  • 1
    It might be worthwhile explaining **exactly** what you're trying to achieve – DarkKnight Jan 12 '22 at 11:06
  • @vish4071 I've edited the code, I was making some changes from my initial code and forgot to edit that part, X is the data I've stated in the starting of the question. Data is in that form and in the do something section I'm modifying the same data. – Sukrut Shishupal Jan 12 '22 at 11:07
  • The return value from `main` is `b1`, will always be of type: `list`. In your 2 threads, in T1, you are passing `987654`, an `int` as argument. In T2, you are passing a `list` as argument – vish4071 Jan 12 '22 at 11:08
  • Btw, none of the loops are infinite. – vish4071 Jan 12 '22 at 11:11
  • ti takes arg a which is an int, and t2 is for the list, which prints the edited value from data (35.6547,56475). t2 has to return value like this once t1 is completed. – Sukrut Shishupal Jan 12 '22 at 11:11
  • for looping I tried using both the methods stated in this post: https://stackoverflow.com/q/23100704/9639867 – Sukrut Shishupal Jan 12 '22 at 11:15
  • 2
    Please post code that's actually runnable with an explanation of what you're trying to achieve. This code makes no sense at all. Why, for example, are you executing *value()* twice? – DarkKnight Jan 12 '22 at 11:22
  • 1
    Despite your latest edit, this code is still not runnable – DarkKnight Jan 12 '22 at 11:33
  • @JCaesar I'm trying to add comments, also please let me know which section is not clear. – Sukrut Shishupal Jan 12 '22 at 11:39
  • 1
    Where is *cds* defined? Why do you call *value()* as a simple function then later as a thread? Why *while True* in *value()*. *value()* cannot run because variable *a* is not defined. You have used *id* as a variable name - that's a Python built-in. Once again I ask, what are you trying to do? Is that a real URL (I can't reach it). If it's private, perhaps you could show an example of the JSON response – DarkKnight Jan 12 '22 at 12:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240995/discussion-between-dustrokes-and-jcaesar). – Sukrut Shishupal Jan 12 '22 at 12:31
  • 1
    I'm in the chatroom – DarkKnight Jan 12 '22 at 12:33

0 Answers0