Centos 8 and Python threading

Issues related to applications and software problems and general support
Post Reply
jamalayka
Posts: 2
Joined: 2020/09/15 11:07:53

Centos 8 and Python threading

Post by jamalayka » 2020/09/15 11:22:53

Hello:

I am trying to run the following python code:

Code: Select all

import concurrent.futures
import logging
import time

class FakeDatabase:
    def __init__(self):
        self.value = 0

    def update(self, name):
        logging.info("Thread %s: starting update", name)
        local_copy = self.value
        local_copy += 1
        time.sleep(0.1)
        self.value = local_copy
        logging.info("Thread %s: finishing update", name)

if __name__ == "__main__":
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO,
                        datefmt="%H:%M:%S")

    database = FakeDatabase()
    logging.info("Testing update. Starting value is %d.", database.value)
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        for index in range(2):
            executor.submit(database.update, index)
    logging.info("Testing update. Ending value is %d.", database.value)

This code simulate a race condition, works fine on Mac, Windows and FreeBSD but not Centos. In Centos, the 2 threads run but they don't seems to finish. ie.e the second logging info in the function update is never reached. The script exits fine.

I no longer have Centos now, but would like to know what is wrong. I also updated Python to 3.8 with same results.
Centos installation is standard, no configuration whatsoever. Updated to latest Centos.

danialbehzadi
Posts: 7
Joined: 2020/08/23 06:40:09

Re: Centos 8 and Python threading

Post by danialbehzadi » 2020/09/16 09:36:40

Did you try using asyncio instead of concurrent?

jamalayka
Posts: 2
Joined: 2020/09/15 11:07:53

Re: Centos 8 and Python threading

Post by jamalayka » 2020/09/16 11:03:45

Hi Danial,

Good idea, but I am not looking for alternative or a workaround. I am in a class and threading and concurrent futures is the subject. These are not working on Centos as they should be as I mentioned in my post. I have now moved on, need to catch up with the class :-).

I just need an explanation, for curiosity more than anything else. Could it be the way Python is configured and built on Centos perhaps?

thanks anyway.

Post Reply