Beispiel #1
0
def test_mongo_exp_with_threads():
    def worker(host_id, n_jobs, timeout):
        mw = MongoWorker(
            mj=TempMongo.mongo_jobs('foodb'),
            )
        t0 = time.time()
        while n_jobs:
            mw.run_one(host_id, timeout)
            print 'worker: ran job'
            n_jobs -= 1

    for bandit_json in ('hyperopt.bandits.GaussWave2',
            'hyperopt.dbn.Dummy_DBN_Base',):
        with TempMongo() as tm:
            assert len(TempMongo.mongo_jobs('foodb')) == 0
            bandit = json_call(bandit_json)
            exp = MongoExperiment(
                bandit_algo=HGP(bandit),
                mongo_handle=tm.mongo_jobs('foodb'),
                workdir=tm.workdir,
                exp_key='exp_key',
                poll_interval_secs=1.0,
                cmd=('bandit_json evaluate', bandit_json))
            exp.ddoc_init()
            assert len(TempMongo.mongo_jobs('foodb')) == 0
            for asdf in exp.mongo_handle:
                print asdf
            assert len(exp.mongo_handle) == 0

            n_trials = 5
            exp.bandit_algo.n_startup_jobs = 3

            wthread = threading.Thread(target=worker,
                    args=(('hostname', 0), n_trials, 660.0))
            wthread.start()
            try:
                print 'running experiment'
                exp.run(n_trials, block_until_done=True)
            finally:
                print 'joining worker thread...'
                wthread.join()

            print exp.trials
            print exp.results
            assert len(exp.trials) == n_trials
            assert len(exp.results) == n_trials

            exp_str = cPickle.dumps(exp)