Not connected to physics server when trying to run environment

Official Python bindings with a focus on reinforcement learning and robotics.
Post Reply
jboyml
Posts: 1
Joined: Mon Jul 22, 2019 8:49 pm

Not connected to physics server when trying to run environment

Post 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?
caffett
Posts: 3
Joined: Wed Nov 13, 2019 9:37 pm

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

Post 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()
caffett
Posts: 3
Joined: Wed Nov 13, 2019 9:37 pm

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

Post 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.
caffett
Posts: 3
Joined: Wed Nov 13, 2019 9:37 pm

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

Post by caffett »

Sorry, this just can fix my issue.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

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

Post 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.
dogbuilder23
Posts: 2
Joined: Mon Mar 14, 2022 5:43 pm

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

Post 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.
dogbuilder23
Posts: 2
Joined: Mon Mar 14, 2022 5:43 pm

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

Post 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.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

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

Post 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
Post Reply