Coder Social home page Coder Social logo

Supress cpymad output to stdio about cpymad HOT 4 CLOSED

mabl avatar mabl commented on July 29, 2024
Supress cpymad output to stdio

from cpymad.

Comments (4)

coldfix avatar coldfix commented on July 29, 2024

Hey,

redirecting the MAD-X output to an arbitrary file handle can be achieved as follows:

from cpymad.madx import Madx
from cpymad import _rpc
import subprocess
import os

def madx_redirect_stdio(stdout=subprocess.STDOUT, stderr=subprocess.STDOUT):
        with open(os.devnull, 'r') as devnull:
            client, process = _rpc.LibMadxClient.spawn_subprocess(
                stdout=stdout,
                stderr=stderr,
                stdin=devnull,
                bufsize=0)         # unbuffered IO
        return Madx(libmadx=client.libmadx)

# drop all stdio:
devnull = open(os.devnull, 'w')
madx = madx_redirect_stdio(devnull, devnull)

See also the code in MadGUI.

I admit, this is a bit hacky and the documentation is almost nonexisting. Probably its a common demand and should be made easier or at least documented. I'll try to add something as soon as I get some time (currently busy with something else, sorry).

Hope, this helps.

EDIT Sorry, had to correct some minor inconsistencies in the original code.

from cpymad.

coldfix avatar coldfix commented on July 29, 2024

I guess I will patch the Madx constructor so that it accepts and forwards keyword arguments stdout and stderr. Hope I get it done by the end of this week.

from cpymad.

coldfix avatar coldfix commented on July 29, 2024

Okay, took me a bit longer to find some time to do this. You can now simply do

m = Madx(stdout=False)

# or

with open('log.txt', 'w') as f:
    m = Madx(stdout=f)

I uploaded a new release on PyPI. Let me know if you have any problems.

Best regards,
Thomas

from cpymad.

mabl avatar mabl commented on July 29, 2024

Thanks! That works great for me!

from cpymad.

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.