openai / gym

A toolkit for developing and comparing reinforcement learning algorithms.
https://www.gymlibrary.dev
Other
34.8k stars 8.61k forks source link

unable to render in forked process on osx. #722

Closed kolt-mcb closed 4 years ago

kolt-mcb commented 7 years ago

I receive the following error when attempting to render any environment on osx.

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().

here is my code

` def jobTrainer(envName):

env = gym.make(envName)
#env = wrappers.Monitor(env,'tmp/'+envName,resume=True,video_callable=False) # no recoding on windows due to ffmepg

if env.action_space.__class__ == gym.spaces.discrete.Discrete: # identifies action/observation space
    discrete = True
else:
    discrete = False

results = []

while not jobs.empty(): # gets a new player index from queue
    try: 
        job = jobs.get()
    except Empty:
        pass

    currentSpecies = job[0]
    currentGenome = job[1]
    genome = job[2]
    attemps = job[3]
    scores = 0
    for run in range(attemps): # runs for number of attemps
        score = 0
        done = False
        ob = env.reset()    

        while not done: 
          ob = obFixer(env.observation_space,ob)
          o = genome.evaluateNetwork(ob,discrete) # evalutes brain, getting button presses
          o = acFixer(env.action_space,o)
          ob, reward, done, _ = env.step(o)
          #env.render() # disabled render
          score += reward

        scores += score
    finalScore = scores/attemps 
    print("species:",currentSpecies, " genome:",currentGenome," Scored:",finalScore)
    results.append((finalScore,job))
env.close()
return results

` and

` def trainPool(envNum,species,queue,env,attemps): # creates multiprocessing job for pool and trains pool

before = time.time()
results = []
jobs = Queue()
s = 0
for specie in species: # generates network for each genome and creates a job with species and genome index, env name and number of trials/attemps

  g=0
  for genome in specie.genomes:
    genome.generateNetwork()
    jobs.put((s,g,genome,attemps))
    g+=1
  s+=1
mPool = multiprocessing.Pool(processes=envNum,initializer =poolInitializer,initargs=(jobs,))
results = mPool.map(jobTrainer,[env]*envNum)
mPool.close()
mPool.join()
for resultChunk in results:
    for result in resultChunk:
        currentSpecies = result[1][0]
        currentGenome = result[1][1]
        species[currentSpecies].genomes[currentGenome].fitness = result[0] # sets results from result list

after = time.time()
print("finished in ",int(after-before))
queue.put(species) # sends message to main tkinter process

`

kolt-mcb commented 7 years ago

you are having an issue running environments in multiprocessing in ubuntu? I use this frequently

christopherhesse commented 5 years ago

This code example isn't easy for me to run, but you probably shouldn't have envs shared across processes. Instead have each process create its own copy of the env, after forking. You can also use a multiprocessing spawn context instead of fork.

christopherhesse commented 5 years ago

Did that fix the issue?

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.