Python Forum
Synchronization/Timing Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Synchronization/Timing Problem
#1
Hello,
The idea is that: I am producing my pairs which is made by bits. Bits are the objects which is produced by another class(this part is ok). After that I am sending them into the ports(this part is ok). And then I am sending them into the inports. And here the problem begins.. In the inportPort class, I have memory which is called by physqueue and I have dataqueu. The reason is we have dataqueue is that: Once A port catch the bit in the memory(physqueue), it is sending a classical message to otherport to say "I have the bit". In the inportProp class, once I catch the bits, I am sending them to the memory and once I catch them in the memory this time I am sending them into the dataqueue. SO timing should be that time in the physqueue < lastt <time in the dataqueue. But the results are totally irrelevant. Sometimes times coming correctly, sometimes times coming totally irrelevant.

when I call my function inside my class, I see that time is decreasing instead of increasing.
class inportProp:
     #Take bits, store them in physical queue and coordinates with the other end via classical links.
     #bit comes at lastt
     #send it to physqueue and in that time, save the time so update time in physqueue
     #finally send it to dataqueue from physqueue so update time in dataqueue
    def __init__(self, t0, tM, incoming, phys, clinkdt, port=None, name=''):
        self.DEBUG=True 
        self.lastt=t0 #lastt is the time for bits and it starts with t0
        self.tM=tM  #this is memory life time
        if self.tM == 0: #this implies infinite memory
            def updateqmem(t):
                pass 
        else :
            def updateqmem(t):
                  
                perase=-expm1(-(t-self.lastt)/self.tM)
                for i,q in enumerate(self.physqueue): 
                    if random()<perase:
                        q.lost() 
                         
                        del self.physqueue[i]
        self.updateqmem=updateqmem
        self.incoming=incoming #incoming bits
        self.phys=phys #maximum bit number in the physqueue
        self.physqueue=[] #physcial queue
        self.dataqueue=[] #just after saving bits in the memory, we send them to dataqueue with delay 
        self.clinkdt=clinkdt #delay between classical message (for dataqueue's)
        self.port=port
        if port != None:
            if port.port==None :
                port.port=self
                port.clinkdt=clinkdt
            else: raise ValueError(f"{port} already ok") 
            self.nextitemin()
            self.port.nextitemin()
        self.name=name
  
    def __next__(self):
        while True:
            if len(self.dataqueue)==0:
                self.otherport.nextitemin() 
            while self.lastt < self.dataqueue[0][0]: 
                self.nextitemin()
            td, cdata = self.dataqueue.pop(0)  #td is the time of bit cdata is the bit in the dataqueue
            self.updateqmem(td)
            self.lastt=td
            while len(self.physqueue)>0 and self.physqueue[0][1].idt<cdata:
                self.physqueue.pop(0) 
            if  len(self.physqueue)>0 and self.physqueue[0][1].idt==cdata:
                tq, qb = self.physqueue.pop(0)  #tq is the time of bit qb is the bit in the physqueue
                return [td, qb]
          
    def nextitemin(self):
        tq, q = next(self.incoming)
        self.updateqmem(tq)
        if self.phys==0 or len(self.physqueue)<=self.phys:
            self.physqueue.append([tq, q]) 
        self.lastt=tq
        self.otherport.dataqueue.append([tq+self.clinkdt, q.idt])
        if self.DEBUG :
            print(f'{self.name}.nextitemin')
            self.showstate()
  
    def showstate(self):
        print(f'{self.name}, t={self.lastt}')
        print(f'Physqueue:{self.physqueue}')
        print(f'Dataqueue:{self.dataqueue}')
Here my codes to run my class

src=source(rate=.4,eta=1)  #I produced the pairs
Abits=link(eta=.8, deltat=1.5, inputs=src.portA)  # I send them into the ports
Bbits=link(eta=0.7, deltat=3.2, inputs=src.portB) # I send them into the ports
Aport=inportProp(t0=0, tM=50, incoming=Abits, phys=5, clinkdt=4,name='A')
Bport=inportProp(t0=0, tM=50, incoming=Bbits, phys=5, clinkdt=4,port=Ainport, name='B')

Aout=[x for x in Aport] 
Bout=[x for x in Bport]
print(f'bits out of A {[x[1].dt for x in Aout]}')
print(f'bits out of B {[x[1].dt for x in Bout]}')
for port in [Ainport, Binport]:
    port.showstate()
My results are here:

Quote:A, t=19.0 Physqueue:[[14.0, <__main__.bit object at 0x7f9888799fd0>], [16.5, <__main__.bit object at 0x7f98887993a0>], [19.0, <__main__.bit object at 0x7f9888880070>]] Dataqueue:[[19.7, (12.5,)], [22.2, (15.0,)]]
bit name=B (12.5,) is lost at t=20.7
B.nextqubitin
B, t=20.7 Physqueue:[[5.7, <__main__.bit object at 0x7f988886c1f0>], [8.2, <__main__.bit object at 0x7f98887a0040>], [10.7, <__main__.bit object at 0x7f9888880eb0>], [18.2, <__main__.bit object at 0x7f98887845e0>], [20.7, <__main__.bit object at 0x7f9888880430>]] Dataqueue:[[10.5, (5.0,)], [13.0, (7.5,)], [15.5, (10.0,)], [18.0, (12.5,)], [20.5, (15.0,)], [23.0, (17.5,)]]
====================
bits out of A [(5.0,), (7.5,)]
bits out of B [(5.0,)]
State of the queus at the end:
A, t=19.0 Physqueue:[[14.0, <__main__.bit object at 0x7f9888799fd0>], [16.5, <__main__.bit object at 0x7f98887993a0>], [19.0, <__main__.bit object at 0x7f9888880070>]] Dataqueue:[[19.7, (12.5,)], [22.2, (15.0,)], [24.7, (17.5,)]]
B, t=10.5 Physqueue:[[10.7, <__main__.bit object at 0x7f9888880eb0>], [18.2, <__main__.bit object at 0x7f98887845e0>], [20.7, <__main__.bit object at 0x7f9888880430>]] Dataqueue:[[13.0, (7.5,)], [15.5, (10.0,)], [18.0, (12.5,)], [20.5, (15.0,)], [23.0, (17.5,)]]
the end******************************************************

Here in the results: the first timing is 20.7 for Bport and dataqueu starts with 10.5. It should be impossible because first the bit arrive to me and I update the time and after that I send it to physqueu and after that I send it to dataqueue . And also The second time of B is coming as 10.5 ..
What is the problem here?
and can a function be written for preventing this event like raise an error?
Reply
#2
You need to improve how you ask questions. You provide no context at all and no interpretation of results. I'm having a hard time making a connection between your "results" and the posted code. I think your "results" are printed by this:
    def showstate(self):
        print(f'{self.name}, t={self.lastt}')
        print(f'Physqueue:{self.physqueue}')
        print(f'Dataqueue:{self.dataqueue}')
But I cannot find "Physqueue:" OR "Dataqueue:" anywhere in the "results". THis leads me to the conclusion that your "results" were not generated using the code in the post.

If you want help you need to make it easy to help you. Ask a real question that someone other than you may have a chance of understanding. Explain what you are trying to do and then explain how the results you are getting differ from the results you expect.
Reply
#3
I editted my post and yess the results were coming the other program, sorry for that. I did not notice that before you wrote...
Hope it is better now...
Reply
#4
You flip back and forth from looking at the front and the rear of the queue. That is why lastt appears to go backwards. The first time you print was after doing appending things to the queue:
        if self.phys==0 or len(self.physqueue)<=self.phys:
            self.physqueue.append([tq, q]) 
        self.lastt=tq
This sets lastt to the most recent time. However, if the if statement evaluates to False, the lasttt was updated by this code in __next__
            td, cdata = self.dataqueue.pop(0)
            self.updateqmem(td)
            self.lastt=td
This updates lasttt to the oldest item in dataqueue.

Your code is really hard to understand. Using better variable names would go a long way to improving readability; lasttt, tM, clinkdt, tq, qb? These would be ok if there was a description anywhere that says what they represent, as is I am left guessing. At the very minimum you should describe each of the arguments passed to inportProp.__init__ along with a short description of what inportProp is/does.
Reply
#5
(Mar-31-2021, 08:23 PM)deanhystad Wrote: You flip back and forth from looking at the front and the rear of the queue. That is why lastt appears to go backwards. The first time you print was after doing appending things to the queue:
        if self.phys==0 or len(self.physqueue)<=self.phys:
            self.physqueue.append([tq, q]) 
        self.lastt=tq
This sets lastt to the most recent time. However, if the if statement evaluates to False, the lasttt was updated by this code in __next__
            td, cdata = self.dataqueue.pop(0)
            self.updateqmem(td)
            self.lastt=td
This updates lasttt to the oldest item in dataqueue.

Your code is really hard to understand. Using better variable names would go a long way to improving readability; lasttt, tM, clinkdt, tq, qb? These would be ok if there was a description anywhere that says what they represent, as is I am left guessing. At the very minimum you should describe each of the arguments passed to inportProp.__init__ along with a short description of what inportProp is/does.

There is an algorithmic problem that I am always walking around...
I gave an explanation in the code for definitions
Reply
#6
(Mar-31-2021, 08:39 PM)quest Wrote:
(Mar-31-2021, 08:23 PM)deanhystad Wrote: You flip back and forth from looking at the front and the rear of the queue. That is why lastt appears to go backwards. The first time you print was after doing appending things to the queue:
        if self.phys==0 or len(self.physqueue)<=self.phys:
            self.physqueue.append([tq, q]) 
        self.lastt=tq
This sets lastt to the most recent time. However, if the if statement evaluates to False, the lasttt was updated by this code in __next__
            td, cdata = self.dataqueue.pop(0)
            self.updateqmem(td)
            self.lastt=td
This updates lasttt to the oldest item in dataqueue.

Your code is really hard to understand. Using better variable names would go a long way to improving readability; lasttt, tM, clinkdt, tq, qb? These would be ok if there was a description anywhere that says what they represent, as is I am left guessing. At the very minimum you should describe each of the arguments passed to inportProp.__init__ along with a short description of what inportProp is/does.

I tried to write expalanation.
There is an algorithmic problem that I am always walking around...
I gave an explanation in the code for definitions
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Timing actions with Python dangermaus33 0 1,003 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Inconsistent counting / timing with threading rantwhy 1 1,758 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Timing of a while loop stylingpat 4 6,797 Mar-31-2021, 10:48 AM
Last Post: stylingpat
  Assigning Data from one column to another with different associated timing interval alexafshari 1 1,948 Apr-30-2020, 03:59 PM
Last Post: pyzyx3qwerty
  Frequency and timing of psycopg2 commits acecase 0 2,077 Nov-01-2019, 05:50 PM
Last Post: acecase
  Perpetual timing Mark17 3 2,869 Oct-24-2019, 03:46 PM
Last Post: Gribouillis
  Timing input Mark17 2 2,285 Oct-23-2019, 08:25 PM
Last Post: Mark17
  Timing functions with multiprocessing mntfr 3 4,998 Nov-18-2018, 06:00 AM
Last Post: woooee
  GPIO output timing help needed skid 5 4,048 Jan-23-2018, 04:12 PM
Last Post: skid
  change timing on py script kwfreverie 2 3,112 Dec-16-2017, 07:35 PM
Last Post: kwfreverie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020