0

I have a python function that gets called everytime i text a specific number. (Postback function, that runs on flask, and listens for incoming sms.

I also have a function that has a set schedule to run every 5minutes and read from a list and remove some entries.

Now the only problem is both these functions read from the same list.

I'm worried that right when the scheduled function (every 5min) is called to be run, the postback function that handles sms, is called too. What i they both run at the exact same time, and both try to read from the array at the same time. Or if function a reads from the list WHILE function b is writing to the list.

Is this an accurate worry of mine? Can these 2 functions be called at the EXACT same time? Or will it never happen that they simultaneously read from same list at same time.

I looked into async requests with celery, but that doesnt solve the problem. The function that runs every 5 min, could still be called the exact same time as the celery function is called, thus corrupting the list.

Thanks, im just confused

TommySmithers
  • 33
  • 1
  • 4

1 Answers1

0

The worries you have are reasonable and should be handled in code.

In general lists are thread safe in python, but that still can cause issues if the data inside your list is not, see this answer:

Are lists thread-safe?

The type of problem you describe is very common and depending on your needs can be solved in several ways:

1) use locks to protect the list: Python threading. How do I lock a thread?

2) rethink the approach and put all your logic that access this particular list into the timer only. Then whenever a text message comes in, you push the info into a queue and read from that queue into the timer. This decouples the two threads. Example from another question (the question tackles another interesting point, but you can just look at the queue usage itself): Threading in python using queue

Peter Branforn
  • 1,679
  • 3
  • 17
  • 29
  • Thanks, ya i was thinking maybe a global variable 'isactive'. So when the post function is called isactive is set to 1 and only when it finishes it sets it to 0. Then in the timer code i just check the status of isactive. Is this a correct approach? Ive been told global variables are very bad practice – TommySmithers Mar 04 '18 at 23:08
  • So you opt for approach 1 then. The 'isactive' solution is not robust, the idea in this approach is to wrap the methods that read and write the list into functions that use a lock to synchronise each other. take a quick look at the link... does this help? – Peter Branforn Mar 04 '18 at 23:15
  • I added another link from within stackoverflow to not have external page linked, it shows how the queue can be used to do this. Its actually typical python to do it this way (I just found out). – Peter Branforn Mar 04 '18 at 23:29
  • Ya i dont think approach 2 solves it. Cause both functions would need to access the queue. This the problem comes up again. Im thinking though with approach 1, what if the lock changes while the timer function is reading from the list. It checks if 'isactive' is set to zero, if it is then start manipulating the list. But what if it is manipulating the list and in the background 'isactive' changes to 1? Then the lock doesn't really work because they'll both be active? – TommySmithers Mar 04 '18 at 23:36
  • Do i need 2 locks to specify which function is active? – TommySmithers Mar 04 '18 at 23:37