Git Product home page Git Product logo

Comments (5)

Moosems avatar Moosems commented on August 20, 2024

I'm looking at the multiprocess module and I may want to move over to that to simplify the codebase and reduce performance overheads. Will need to investigate further

from salve.

Moosems avatar Moosems commented on August 20, 2024

Yes, it seems that the multiprocess library is just a builtin form of IPC specifically for python :D

from salve.

Moosems avatar Moosems commented on August 20, 2024

The send is near instant with even 140k char strings. I will definitely look into rewriting the basic example and then the main library piece by piece

from salve.

Moosems avatar Moosems commented on August 20, 2024

Should also restore full windows compatability

from salve.

Moosems avatar Moosems commented on August 20, 2024

Got this working:

from multiprocessing import Process, Pipe, Queue
from multiprocessing.connection import Connection
from time import sleep, time
from typing import Any, NotRequired, TypedDict
from random import randint
from sys import exit


class Message(TypedDict):
    """Base class for messages in and out of the server"""

    id: int


class Request(Message):
    """Request results/output from the server"""

    index: int


class Response(Message):
    """Server responses to requests, notifications, and pings"""

    cancelled: bool
    result: NotRequired[str]


class Client:
    def __init__(self) -> None:
        self.used_ids: list[int] = []
        self.current_id: int = 0
        self.client_end: Connection
        self.response_queue: Queue[Response] = Queue()
        self.requests_queue: Queue[Request] = Queue()
        self.create_server()

    def create_server(self) -> None:
        self.client_end, server_end = Pipe()
        self.proccess = Process(
            target=Server,
            args=(server_end, self.response_queue, self.requests_queue),
        )
        self.proccess.start()
        self.response_queue.get()  # Wait until server has been made

    def check_server(self) -> None:
        if not self.proccess.is_alive():
            self.create_server()

    def make_request(self, request: int) -> None:
        self.check_server()
        id = randint(0, 5000)
        while id in self.used_ids:
            id = randint(0, 5000)

        self.used_ids.append(id)
        self.current_id = id
        final_request: Request = {"id": id, "index": request}
        self.requests_queue.put(final_request)

    def check_output(self) -> Any:
        items: list[Response] = []
        while not self.response_queue.empty():
            items.append(self.response_queue.get())

        wanted_response: Response = {"id": 0, "cancelled": True}
        for item in items:
            id: int = item["id"]
            self.used_ids.remove(id)
            if id == self.current_id:
                wanted_response = item
        return wanted_response

    def kill_server(self) -> None:
        self.proccess.kill()


class Server:
    def __init__(
        self,
        server_end: Connection,
        response_queue: Queue,
        requests_queue: Queue,
    ) -> None:
        self.internal_list: list[str] = ["Yuh", "anyways", "soooo"]
        self.server_end: Connection = server_end
        self.response_queue: Queue[Response] = response_queue
        self.requests_queue: Queue[Request] = requests_queue
        self.response_queue.put({"id": 0, "cancelled": False, "result": ""})
        self.old_time = time()

        while True:
            self.run_tasks()

    def run_tasks(self) -> None:
        current_time = time()
        if current_time - self.old_time > 5:
            exit(0)

        while not self.requests_queue.empty():
            self.old_time = current_time
            request: Request = self.requests_queue.get()
            cancelled: bool = not self.response_queue.empty()
            output = ""
            if not cancelled:
                try:
                    output = self.internal_list[request["index"]]
                except IndexError:
                    output = "Bad Index"
            final_response: Response = {
                "id": request["id"],
                "cancelled": cancelled,
                "result": output,
            }
            self.response_queue.put(final_response)

        sleep(0.0025)


if __name__ == "__main__":
    st = time()
    c = Client()
    c.make_request(3)
    while (x := c.check_output()) is None or x == {"id": 0, "cancelled": True}:
        continue
    print(x)
    c.kill_server()
    print(time() - st, c.__sizeof__())

from salve.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.