Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gurobi and gurobipy - doubled output console when using Python logging

When using the Gurobi Python package (gurobipy v. 8.1.0) alongside the standard Python logging package, I get on console a doubled output for Gurobi, for example

Total elapsed time = 498.27s
[2019-03-04 17:51:58,804][INFO] Total elapsed time = 498.27s

Does anybody know how to remove logging for gurobipy? Thanks

like image 251
user1403546 Avatar asked Oct 16 '25 19:10

user1403546


1 Answers

I had this problem as well. Couldn't find an "official" solution so I rolled my own hack:

import sys

def solve():
    class DevNull:
        def write(self, *args, **kwargs):
            pass

        def flush(self, *args, **kwargs):
            pass

    sys.stdout = DevNull() 

    try:
        return _actually_solve()
    except Exception:
        # restore stdout so that handlers can print normally
        # https://docs.python.org/3/library/sys.html#sys.__stdout__ 
        sys.stdout = sys.__stdout__
        raise
    finally:
        sys.stdout = sys.__stdout__

This function wraps the actual solving logic in _actually_solve and replaces the standard output with a file-like object that ignores everything we write there.

like image 126
BlackBear Avatar answered Oct 19 '25 10:10

BlackBear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!