parallel generation of voxels (Python)

I tried to do threading to load chunk of voxels It worked in series but when implemented in parallel threading it was not working like I wanted it, I even used Chatgpt and other AI tools but It didn’t help as expected Here is my code (please don’t judge It’s been a little edited by AI XD)

import threading
import queue
from settings import *
from world_objects.chunk import Chunk
from voxel_handler import VoxelHandler


class World:
    def __init__(self, app):
        self.app = app
        self.world_height = WORLD_H + BUILD_C_HEIGHT_LIMIT
        self.chunks = {}
        self.voxel_pointers = np.empty([WORLD_W * WORLD_D * self.world_height, CHUNK_VOLUME], dtype='uint8')
        self.voxel_handler = VoxelHandler(self)
        self.render_distance = 7

        self.chunk_queue = queue.Queue()
        self.main_thread_queue = queue.Queue()
        self.stop_threads = False

        self.update_active_chunks()

        self.chunk_thread = threading.Thread(target=self.chunk_worker, daemon=True)
        self.chunk_thread.start()

    def update(self):
        self.voxel_handler.update()
        self.update_active_chunks()
        self.process_main_thread_tasks()

    def chunk_worker(self):
        while not self.stop_threads:
            try:
                pos = self.chunk_queue.get(timeout=1)
                print(f"Generating chunk at {pos}")  # Debug print
                self.generate_chunk_data(pos)
                self.chunk_queue.task_done()
            except queue.Empty:
                continue

    def process_main_thread_tasks(self):
        while not self.main_thread_queue.empty():
            task = self.main_thread_queue.get()
            task()
            self.main_thread_queue.task_done()

    def update_active_chunks(self):
        player_pos = self.app.player.position
        print(f"Player position: {player_pos}")

        player_chunk_pos = (
            int(player_pos[0] // CHUNK_SIZE),
            int(player_pos[1] // CHUNK_SIZE),
            int(player_pos[2] // CHUNK_SIZE)
        )

        render_x_min = max(0, player_chunk_pos[0] - self.render_distance)
        render_x_max = min(WORLD_W, player_chunk_pos[0] + self.render_distance)
        render_y_min = max(0, player_chunk_pos[1] - self.render_distance)
        render_y_max = min(self.world_height, player_chunk_pos[1] + self.render_distance)
        render_z_min = max(0, player_chunk_pos[2] - self.render_distance)
        render_z_max = min(WORLD_D, player_chunk_pos[2] + self.render_distance)

        active_chunk_positions = {
            (x, y, z)
            for x in range(render_x_min, render_x_max)
            for y in range(render_y_min, render_y_max)
            for z in range(render_z_min, render_z_max)
        }

        print(f"Active chunk positions: {active_chunk_positions}")

        self.load_and_unload_chunks(active_chunk_positions)

    def load_and_unload_chunks(self, active_chunk_position):
        current_chunk_positions = set(self.chunks.keys())

        chunks_to_load = active_chunk_position - current_chunk_positions
        chunks_to_unload = current_chunk_positions - active_chunk_position

        for pos in chunks_to_load:
            print(f"Queueing chunk for loading at {pos}")
            self.chunk_queue.put(pos)

        for pos in chunks_to_unload:
            self.unload_chunk(pos)

    def generate_chunk_data(self, pos):
        # Generate chunk data without OpenGL operations
        x, y, z = pos
        chunk = Chunk(self, position=pos)
        chunk_index = x + WORLD_W * z + WORLD_AREA * y

        self.voxel_pointers[chunk_index] = chunk.build_voxels()
        chunk.voxels = self.voxel_pointers[chunk_index]

        # Add chunk to chunks dictionary (thread-safe)
        with threading.Lock():
            self.chunks[pos] = chunk

        # Queue the chunk for mesh building in the main thread
        self.app.queue_main_thread_task(lambda: self.build_chunk_mesh(chunk))

    def build_chunk_mesh(self, chunk):
        try:
            chunk.build_mesh()
            print(f"Chunk mesh built at {chunk.position}")
        except Exception as e:
            print(f"Error building mesh for chunk at {chunk.position}: {e}")
            self.unload_chunk(chunk.position)

    def unload_chunk(self, pos):
        if pos in self.chunks:
            print(f"Unloading chunk at {pos}")
            del self.chunks[pos]

    def render(self):
        for chunk in self.chunks.values():
            if chunk and chunk.mesh and chunk.mesh.program:
                print(f"Rendering chunk at {chunk.position}")  # Debug print
                chunk.render()
            else:
                if chunk:
                    print(f"Chunk at {chunk.position} not rendered due to uninitialized mesh or program.")
                else:
                    print(f"Chunk is None")

    def queue_main_thread_task(self, task):
        self.main_thread_queue.put(task)

    def shutdown(self):
        self.stop_threads = True
        self.chunk_thread.join()

I use moderngl, pygame, numpy, numba There are no voxels or chunks creating in this can someone please help I can’t sleep at night thinking about what I did wrong

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật