|Python 3.11 Released|
|Written by Mike James|
|Wednesday, 26 October 2022|
Python 3.11 is now officially the latest stable version of Python and it is time to see what the new kid on the block offers - mainly speed, but there are some other important additions.
The big improvement is simply speed. Previously we reported that the official documentation claimed an improvement of 25% for 3.11 over 3.10. The testing site Phoronix conducted an extensive test in June 2022 and reported that the current Python 3.11 beta was 45% faster than 3.10 and 48% faster than the older 3.8. This is impressive. The release is claimed to be between 10 to 60% faster. You can see a list of improvements at Faster CPython. For me the faster startup is particularly impressive.
As previously stated there is no movement on the important issue of removing the GIL from Python. The Global Interpreter Lock, usually known as the GIL, restricts the Python interpreter to only running a single thread at a time, even when running on a multicore machine. This is one of the big disappointments for anyone wanting to speed things up. The GIL, even though it slows things down for advanced users, keeps things fast for the majority of programs which are single-threaded. It was supposed that if the GIL can be removed without slowing things down then it was worth doing. This was demonstrated to be possible, but only by improving Python to make up for the resulting slow down. Now that many of these improvements have been incorporated into 3.11 removing the GIL has a new and even more difficult target to hi
Other improvements are small but welcome.
Most programs try not to raise any exceptions, but now you can raise and handle a group of exceptions. As if one exception at a time isn't bad enough! The idea is that sometimes an error isn't a single thing that needs fixing. Complex errors might involve a range of different suberrors. For example, perhaps a read error on a file renders a particular calculation impossible - then you need to raise two exceptions to get the handling code to fix both problems. Similarly multiple exceptions can occur in concurrent code. Now you can define an exception group which can be raised in a single operation and caught using the new except* syntax.
Also new is the asynchronous task group. This provides an easy way to create and wait for a group of tasks. For example:
async with asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...)) print("Both tasks have completed now.")
When the async with exits the thread waits for all of the tasks in the group to complete. If any task fails with an exception then all tasks are cancelled. If you are familiar with asynico you may realize that this is a slightly more structured alternative to using gather. Task groups are said to be a more modern approach and they lead to the idea of "structured concurrency". If you only use task groups then you can create nested trees of tasks that complete in a predictable way.
The big difference between groups and gather is the way that exceptions are handled. The gather awaitable by default returns the first exception, but the other tasks continue to run. You can also opt to allow all tasks to complete and treat any exceptions as normal results. In addition gather returns the results of the tasks as a list - task groups don't. Clearly task groups are all about stopping tasks if one of the group fails. I think most of my code will continue to use gather even if it is more difficult to ensure that all of the tasks end cleanly.
There are some additions to the typing features, but as I don't think Python needs typing I'm not impressed. In fact, the idea that I can now define variadic generics indicates what a mess you quickly get into with this sort of typing. Keep it simple is my motto.
Of course, if you have drunk the typing kool aid you will welcome the additions.
Finally there is a new library module, tomllib - which makes working with TOML files easier. What is TOML? It is a configuration language or a data definition language depending on your viewpoint. It is like JSON but easier to read - aka even more verbose. It is reminiscent of Window's INI files and personally I'm going to continue to use JSON even though the brackets and commas can be confusing, but I might be tempted to try it in a new project.
There are also a number of deprecated libraries - none that are likely to be controversial, but still worth checking that they don't affect your plans.
Python 3.11 Performance Benchmarks Are Looking Fantastic
Python multithreading without the GIL
PEP 659 -- Specializing Adaptive Interpreter
Guido And Microsoft Want To Make Python x2 Faster
Microsoft Now Visionary Sponsor Of Python
What Makes Python Great & Greater
Guido van Rossum Quits As Python BDFL
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
or email your comment to: email@example.com
|Last Updated ( Wednesday, 26 October 2022 )|