from __future__ import print_function

import threading
import time

from sigopt.interface import Connection

# Just fill these in with your credentials
CLIENT_TOKEN = 'XXXXXXXXXX'
EXPERIMENT_ID = 0

class ExampleRunner(threading.Thread):
  def __init__(self, worker_id):
    threading.Thread.__init__(self)
    self.worker_id = worker_id
    self.connection = Connection(client_token=CLIENT_TOKEN, worker_id=self.worker_id)

  def run(self):
    while True:
      assignments = self.connection.experiments(EXPERIMENT_ID).suggest().suggestion.assignments
      print('{0} - Evaluating at parameters: {1}'.format(self.worker_id, assignments))
      metric_value = self.evaluate(assignments)
      print('{0} - Observed value: {1}'.format(self.worker_id, metric_value))
      self.connection.experiments(EXPERIMENT_ID).report(data={
        'assignments': assignments,
        'value': metric_value,
      })

  def evaluate(self, assignments):
    """
    Replace this with the function you want to optimize
    This fictitious example has only two parameters, named param1 and param2
    """
    sleep_seconds = 10
    print('{0} - Sleeping for {1} seconds to simulate expensive computation...'.format(self.worker_id, sleep_seconds))
    time.sleep(sleep_seconds)
    return assignments['param1'] - assignments['param2']

if __name__ == '__main__':

  RUNNER_COUNT = 2
  runners = [ExampleRunner('worker-' + str(i+1)) for i in range(RUNNER_COUNT)]

  for runner in runners:
    runner.daemon = True
    runner.start()

  while True:
    time.sleep(1)
    if all((not r.is_alive() for r in runners)):
      break

@@ -32,16 +32,32 @@
 nction%0A%0A
+import argparse%0A
 import t
@@ -81,138 +81,235 @@
 ime%0A
-%0Afrom sigopt.interface import Connection%0A%0A# Just fill these in with your credentials%0ACLIENT_TOKEN = 'XXXXXXXXXX'%0AEXPERIMENT_ID = 0
+# insert your CLIENT_ID, CLIENT_TOKEN, USER_TOKEN into sigopt_creds.py%0A# otherwise you'll see %22This endpoint requires an authenticated user%22 errors%0Afrom sigopt_creds import CLIENT_TOKEN%0A%0Afrom sigopt.interface import Connection%0A
 %0A%0Acl
@@ -366,22 +366,26 @@
 _(self, 
-worker
+experiment
 _id):%0A  
@@ -422,39 +422,8 @@
 lf)%0A
-    self.worker_id = worker_id%0A
     
@@ -480,35 +480,48 @@
 OKEN
-, worker_id=self.worker
+)%0A    self.experiment_id = experiment
 _id
-)
 %0A%0A  
@@ -557,27 +557,26 @@
 :%0A      
-assignments
+suggestion
  = self.
@@ -602,55 +602,50 @@
 nts(
-EXPERIMENT_ID).suggest().suggestion.assignments
+self.experiment_id).suggestions().create()
 %0A   
@@ -698,32 +698,55 @@
 .format(
-self.worker_id, 
+threading.current_thread(), suggestion.
 assignme
@@ -757,23 +757,16 @@
 )%0A      
-metric_
 value = 
@@ -778,17 +778,35 @@
 evaluate
-(
+_metric(suggestion.
 assignme
@@ -861,31 +861,36 @@
 mat(
-self.worker_id, metric_
+threading.current_thread(), 
 valu
@@ -931,71 +931,83 @@
 nts(
-EXPERIMENT_ID).report(data=%7B%0A        'assignments': assignments
+self.experiment_id).observations().create(%0A        suggestion=suggestion.id
 ,%0A  
@@ -1016,24 +1016,14 @@
     
-'
 value
-': metric_
+=
 valu
@@ -1031,17 +1031,16 @@
 ,%0A      
-%7D
 )%0A%0A  def
@@ -1048,16 +1048,23 @@
 evaluate
+_metric
 (self, a
@@ -1339,22 +1339,34 @@
 mat(
-self.worker_id
+threading.current_thread()
 , sl
@@ -1498,27 +1498,281 @@
 _':%0A
-%0A  RUNNER_COUNT = 2
+  parser = argparse.ArgumentParser()%0A  parser.add_argument('--runner_count', type=int, default=2)%0A  parser.add_argument('--experiment_id', type=int)%0A  the_args = parser.parse_args()%0A%0A  if the_args.experiment_id is None:%0A    raise Exception(%22Must provide an experiment id%22)%0A
 %0A  r
@@ -1799,35 +1799,37 @@
 ner(
-'worker-' + str(i+1)
+the_args.experiment_id
 ) for 
-i
+_
  in 
@@ -1838,20 +1838,29 @@
 nge(
-RUNNER_COUNT
+the_args.runner_count
 )%5D%0A%0A
@@ -1935,71 +1935,17 @@
 %0A%0A  
-while True:%0A    time.sleep(1)%0A    if all((not r.is_alive() for 
+for runne
 r in
@@ -1956,20 +1956,24 @@
 ners
-))
 :%0A    
-  break
+runner.join()
 %0A

update example code