Python

7643 readers
40 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
401
402
403
404
 
 

In this blog post, I will look into the implementation details of CPython’s Global Interpreter Lock (GIL) and how they changed between Python 3.9 and the current development branch that will become Python 3.13.

The general approach is that a Python thread obtains the GIL when it starts executing Python bytecode. It will hold the GIL as long as it needs to and eventually release it, for instance when it is done executing, or when it is executing some operation that often would be long-running and itself does not require the GIL for correctness. This includes for instance the aforementioned file reading operation or more generally any I/O operation. However, a thread may also release the GIL when executing specific bytecodes.

This is where Python 3.9 and 3.13 differ substantially. Let’s start with Python 3.13, which I think roughly corresponds to what Python has been doing since version 3.10 (roughly since this PR). Here, the most relevant bytecodes are for function or method calls as well as bytecodes that jump back to the top of a loop or function. Thus, only a few bytecodes check whether there was a request to release the GIL.

In contrast, in Python 3.9 and earlier versions, the GIL is released at least in some situations by almost all bytecodes. Only a small set of bytecodes including stack operations, LOAD_FAST, LOAD_CONST, STORE_FAST, UNARY_POSITIVE, IS_OP, CONTAINS_OP, and JUMP_FORWARD do not check whether the GIL should be released.

For Python 3.13, this should mean that a function that contains only bytecodes that do not lead to a CHECK_EVAL_BREAKER() check should be atomic.

For Python 3.9, this means a very small set of bytecode sequences can be atomic, though, except for a tiny set of specific cases, one can assume that a bytecode sequence is not atomic.

405
 
 

Out of the core dev sprint. See mastodon thread for additional posts from the event: https://mastodon.social/@hugovk/111221035410194967

I like the focus on having a clean and simple build and implementation.

I’m unclear however on what kind of performance improvements can be expected. The speaker mentions a benchmark against luaJit that was 35% slower as well as some others, but I didn’t pick up on any estimates specific to Python. Maybe lua and Python are similar enough, I personally don’t know.

406
 
 

This is a discussion on Python's forums about adding something akin to a throws keyword in python.

407
408
409
410
 
 

I'll give you one advice, kids. Don't agree if someone offers you to work with odoo.
It's a fucking pain in development.

#python @python

411
412
413
 
 

Edit: The key was using msys2. After installing Gtk3 and PyGObject following the PyGObject guide for msys2 everything worked fine. Packaging with PyInstaller and nuitka was fine.

I've been developing an image halftoning app for a while using GTK on Linux, thinking GTK is cross platform, and delaying testing it on Windows for quite some time.

Today I decided to finally install Windows 10 (for the first time in more than a decade) on a separate machine and see what works and what does not.

For the past few hours I've been through hell trying to get GTK3 to work. I've followed multiple guides, none of which worked, including wingtk.

Furthermore, even if I successfully compile (or install) GTK, would it be possible to package it all up using something like PyInstaller or nuitka.

At this point I'm thinking of keeping the functions and writing a whole new GUI in Tk for the Windows port, as at least that seems to work.

Am I missing something?

414
415
416
417
7
submitted 2 years ago* (last edited 2 years ago) by fzz@programming.dev to c/python@programming.dev
 
 

I've just create a little script to install pip in the latest Pythonista with comfort configuration, as I suppose.

This could probably be useful to someone other than me.

screenshotsIMG_0572 1 2 3


❀️‍πŸ”₯

418
 
 

I want to build a python package using setuptools. The folder structure of the project is the following (some non-essential parts have been deleted):

energy-monitor
β”œβ”€β”€ config
β”‚ β”œβ”€β”€ config.yml
β”‚ └── secrets.yml
β”œβ”€β”€ data
β”‚ └── cpu_tdp.json
β”œβ”€β”€ energy_monitor
β”‚ β”œβ”€β”€ core
β”‚ β”‚ β”œβ”€β”€ gui.py
β”‚ β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ data
β”‚ β”‚ └── tableExport.json
β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ main.py
β”‚ └── utils
β”‚     β”œβ”€β”€ api_calls.py
β”‚     └── __init__.py
β”œβ”€β”€ energy-monitor.py
β”œβ”€β”€ LICENSE
β”œβ”€β”€ MANIFEST.in
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ README.md
└── requirements.txt

The content of the pyproject.toml file is the following (some non-essential parts have been deleted):

[build-system]
requires = ["setuptools>=68.0"]
build-backend = "setuptools.build_meta"

[project]
name = "energy_monitor"
version = "0.0.1"
description = "Energy monitor"
readme = "README.md"
requires-python = ">=3.11"
license = {text = "GPLv3"}
classifiers = [
  "Programming Language :: Python :: 3",
  "Operating System :: OS Independent",
]
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[tool.setuptools]
packages = [
  "energy_monitor", 
  "energy_monitor.core", 
  "energy_monitor.utils"
]
include-package-data = true

[project.scripts]
energy-monitor = "energy_monitor.main:main"

Finally, the content of the MANIFEST.in file is the following:

include README.md
include LICENSE
graft config

I generate the package with python -m build and install the .tar.gz archive with pipx. According to setuptools documentation, I expect to find my config folder, together with README and LICENSE in the interpreter directory (site-packages) after the installation. However, this doesn't happen and I cannot run the app becase it complains that it doesn't find the config. What am I missing?

419
420
421
4
Permanently Deleted (programming.dev)
submitted 2 years ago* (last edited 2 years ago) by CoderSupreme@programming.dev to c/python@programming.dev
 
 

Permanently Deleted

422
 
 

I can't give you the code because this is work related, and I couldn't make a minimal code that mimics the behavior.

My of my programs at work is a log parser, it reads syslog and use compiled regex pattern to match syslog, the whole program looks like this

If match := pattern.match(line):
  Do this
elif match := pattern2.match(line):
  Do that
…

During almost two years of me developing the programs, there are more patterns and more things to do and it gets slower.

But I recently figure out that if I commented out Do that and replace it with pass, my program speeds up, even if pattern2 never matches in my test case.

More strange thing is that in my attempts to use cProfile to profile things, my program runs 2.5x to 3x faster by just doing

from cProfile import Profile
with Profile() as profile:
  main()

I don't even call anything with profile variable and it speeds up my program, why is that?

423
424
 
 
  • Python 3.12 support.
  • Python 3.7 is no longer supported.
  • Shiboken generator is now available on PyPI.
  • Better asyncio compatibility.
  • Better Android compatibility.
  • aarch64 architecture is now supported.
  • Better access to commercial wheels.
  • Better Python integration in Qt Creator.
  • Out to date research plan for dynamic binding.
425
 
 

The thing I love about python is it's elegance; and I thing that is partially due to its syntactic sugar. Between list comprehensions, destructuring, enumerators, generators with yield, and a bunch more, what is your favorite

view more: β€Ή prev next β€Ί