Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supress cpymad output to stdio #15

Closed
mabl opened this issue Jun 9, 2015 · 4 comments
Closed

Supress cpymad output to stdio #15

mabl opened this issue Jun 9, 2015 · 4 comments

Comments

@mabl
Copy link

mabl commented Jun 9, 2015

When doing parameter sweeps, the output of madx pollutes the standard output.
I have looked and found no way in the documentation to suppress or redirect that output.

@coldfix
Copy link
Member

coldfix commented Jun 9, 2015

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.

@coldfix
Copy link
Member

coldfix commented Jun 9, 2015

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.

@coldfix
Copy link
Member

coldfix commented Jun 21, 2015

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

@coldfix coldfix closed this as completed Jun 21, 2015
@mabl
Copy link
Author

mabl commented Jul 25, 2015

Thanks! That works great for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants