2

What guarantees are provided for this code execution in the case of using multithreading in Python:

a=b

Where a, b are integer variables (i.e. they have class 'int' in a Python Programming language). Also "a", "b" are variables from a global context.

Q1: I think it's not necessary that this instruction be translated into something like "mov [a], [b]" (in x86). If write code in ASM x86 there is a possibility to specify "lock mov [a], [b]". Of course, it's too low level and scripting language like Python, I bet, does a lot of instruction on top.

But still, some language like C++ has volatile keyword which will restrict compiler to create instruction which for optimization purpose store variables in a CPU registers.

How in Python language specify that? Is it possible?

Q2: With this line of code, there is another problem, even if it's possible to create some form of volatile behavior (maybe), but another question how to put Memory Fence. For people who are working with Python/Bash and less with compile languages, there is a thing that threads may execute in separate CPU cores, and sometimes you maybe not so lucky and will read the incorrect values from the CPU core cache. In compiling language which is translated in x86 or ARM instruction it's you that responsible for that at the end of the day, but for Python - I have no idea who is responsible for that.

3
  • Before closing the question please provide exact references with answers for people whose million UDS startups depend on such things. Commented Apr 2, 2021 at 14:04
  • I did not found in documentation answers for my question. And I don't see the volatile keywords in this section. Yes threads should be in the same process, it's a meaning of the word in a classical meaning. My question is about memory access garantees to shared integer variable. You can open section docs.python.org/3/library/threading.html and verify that there is no mentioning of "atomic", "volatile", etc. Commented Apr 2, 2021 at 14:21
  • I'm asking about threads within one interpreter process when they execute this single line of code. If know answers for Q1 or Q2 please let me know. Thanks. Commented Apr 2, 2021 at 14:22

1 Answer 1

1

You cannot make any general statements about the execution of a program that is written in Python, as it depends too much on the implementation details of the interpreter that is being used (CPython, Jython, IronPython, etc.), or the libraries that are used by the program. As far as I know, the Python language itself barely makes any promises at this level, and doesn't expose any relevant mechanics, with the exception of for high-level concepts such as threads, processes, mutexes, etc.

If you're concerned about the execution of your program, then simply don't use just Python. Instead, you should implement the critical parts of your codebase in C or C++, and use Python bindings to interact with those components. That way, you can manage memory and execution problems at the appropriate level with programming languages that are better suited, while also being able to use Python to tie most of the codebase together.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeap, I also have such fillings...But Python starts to be considered as not only a scripting language that is used to launch processes or filter strings with regexp but something like programming language...People create pretty big systems in Python. Another aspect if I will create own CPython what are the rules?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.