Page 1 of 1

Not connected to physics server when trying to run environment

Posted: Mon Jul 22, 2019 8:52 pm
by jboyml
Hi,

I'm trying to use PyBullet for reinforcement learning and can't get started with a simple example. The code below fails with the following error:
Traceback (most recent call last):
File "test.py", line 10, in <module>
observation, reward, done, info = env.step(action)
File "/home/john/anaconda3/lib/python3.6/site-packages/gym/wrappers/time_limit.py", line 15, in step
observation, reward, done, info = self.env.step(action)
File "/home/john/anaconda3/lib/python3.6/site-packages/pybullet_envs/gym_pendulum_envs.py", line 31, in step
self.robot.apply_action(a)
File "/home/john/anaconda3/lib/python3.6/site-packages/pybullet_envs/robot_pendula.py", line 25, in apply_action
self.slider.set_motor_torque(100 * float(np.clip(a[0], -1, +1)))
File "/home/john/anaconda3/lib/python3.6/site-packages/pybullet_envs/robot_bases.py", line 381, in set_motor_torque
self.set_torque(torque)
File "/home/john/anaconda3/lib/python3.6/site-packages/pybullet_envs/robot_bases.py", line 387, in set_torque
force=torque)#, positionGain=0.1, velocityGain=0.1)
pybullet.error: Not connected to physics server.

Code: Select all

import gym
import pybullet, pybullet_envs

env = gym.make('InvertedPendulumBulletEnv-v0')
observation = env.reset()

for _ in range(1000):
    env.render()
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)

    if done:
        observation = env.reset()
    env.close()
I'm using Python 3.6.7 and PyBullet 2.5.2 installed using pip on Ubuntu. Any ideas?

Re: Not connected to physics server when trying to run environment

Posted: Wed Nov 13, 2019 9:40 pm
by caffett
Similar bug.

Code: Select all

# -*- coding: utf-8 -*-
import pybullet as p
import pybullet_envs
import gym


def show_bug():
	env = gym.make("AntBulletEnv-v0")
	print(env.reset())
	print(p.getNumBodies())
	env.step(env.action_space.sample())
	env.close()

	env = gym.make("AntBulletEnv-v0")
	print(env.reset())
	print(p.getNumBodies())
	env.step(env.action_space.sample())
	env.close()
	
	env = gym.make("AntBulletEnv-v0")
	print(p.getNumBodies())
	print(env.reset())
	env.step(env.action_space.sample())
	env.close()

	print("end!")

if __name__ == "__main__":
	show_bug()

Re: Not connected to physics server when trying to run environment

Posted: Thu Nov 14, 2019 5:24 am
by caffett
https://github.com/bulletphysics/bullet ... y#L18-#L22

Comment this code and replace them with a pass, everything will be fine.

The python garbage collection mechanism will not delete the variable immediately. Thus, there will be a delay to call the __del__. When the __del__ is called, it will close the current connection, however, due to the delay, the connection might not be the connection we want to close at begin.

Re: Not connected to physics server when trying to run environment

Posted: Thu Nov 14, 2019 5:30 am
by caffett
Sorry, this just can fix my issue.

Re: Not connected to physics server when trying to run environment

Posted: Sat Jun 06, 2020 6:35 am
by Erwin Coumans
caffett wrote: Thu Nov 14, 2019 5:24 am Comment this code and replace them with a pass, everything will be fine.
The pybullet connection for those envs are created in the env.reset() method.
You are making a call to global pybullet.getNumBodies before calling the reset, which is wrong.
Also, it is unsafe to use global pybullet together with a bullet_client.

Code: Select all

env = gym.make("AntBulletEnv-v0")
print(p.getNumBodies())
print(env.reset())
Change the code to this:

Code: Select all

env = gym.make("AntBulletEnv-v0")
print(env.reset())
print(p.getNumBodies())
The delayed __del__ is not an issue at all, since the env.close() calls the disconnect.

Re: Not connected to physics server when trying to run environment

Posted: Mon Mar 14, 2022 5:49 pm
by dogbuilder23
I'm getting this error when trying to run
https://github.com/OpenQuadruped/spot_m ... pot_ars.py

regardless of the flag values I choose.

I'm running
Python 3.8.9
pybullet build time: Dec 27 2021 18:13:03
MacOS 12.0.1, on a MacBook Pro (16-inch, 2021) M1 Max.

I'm a PyBullet noob. Any advice would be appreciated.

Re: Not connected to physics server when trying to run environment

Posted: Mon Mar 14, 2022 5:52 pm
by dogbuilder23
Forgot to mention, the other script from the same github repo work fine.
E.g.
spot_ars_eval.py
env_tester.py

The problem seems to be when spot_ars.py is using multiprocessing workers for the training.

Re: Not connected to physics server when trying to run environment

Posted: Tue Mar 15, 2022 2:44 am
by Erwin Coumans
Please check out those examples, they show how to implement multiprocessing with PyBullet, you need to be careful:

https://github.com/bulletphysics/bullet ... bots/panda