{"action":"opened","issue":{"url":"https://api.github.com/repos/jpieper/pygazebo/issues/24","labels_url":"https://api.github.com/repos/jpieper/pygazebo/issues/24/labels{/name}","comments_url":"https://api.github.com/repos/jpieper/pygazebo/issues/24/comments","events_url":"https://api.github.com/repos/jpieper/pygazebo/issues/24/events","html_url":"https://github.com/jpieper/pygazebo/issues/24","id":95028251,"number":24,"title":"Possible race condition?","user":{"login":"Skylion007","id":2053727,"avatar_url":"https://avatars.githubusercontent.com/u/2053727?v=3","gravatar_id":"","url":"https://api.github.com/users/Skylion007","html_url":"https://github.com/Skylion007","followers_url":"https://api.github.com/users/Skylion007/followers","following_url":"https://api.github.com/users/Skylion007/following{/other_user}","gists_url":"https://api.github.com/users/Skylion007/gists{/gist_id}","starred_url":"https://api.github.com/users/Skylion007/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Skylion007/subscriptions","organizations_url":"https://api.github.com/users/Skylion007/orgs","repos_url":"https://api.github.com/users/Skylion007/repos","events_url":"https://api.github.com/users/Skylion007/events{/privacy}","received_events_url":"https://api.github.com/users/Skylion007/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-14T20:05:49Z","updated_at":"2015-07-14T20:05:49Z","closed_at":null,"body":"So I am wrote a program to apply some joint PIDs when the server start. However, it only works when I put .sleep between successive Trollius calls and when I put Trollius sleep of .1 BEFORE the first publisher.message. Any idea why this works only in this configuration? I would like to note that it also works if you shove everything into one giant event loop, but otherwise it does not work. Any idea what could be causing it? I fear it might be a race condition on Trollius\r\n\r\nHere is the code:\r\n\r\n\r\n    # -*- coding: utf-8 -*-\r\n    \"\"\"\r\n    Created on Tue Jul 14 13:27:48 2015\r\n    \r\n    @author: skylion\r\n    \"\"\"\r\n    import trollius #NOTE: Trollius requires protobuffer from Google\r\n    from trollius import From\r\n    \r\n    import pygazebo\r\n    import pygazebo.msg.joint_cmd_pb2\r\n    import time \r\n    import popupcad\r\n    from lxml import etree\r\n    import subprocess\r\n    import os\r\n    \r\n    try:\r\n        import itertools.izip as zip\r\n    except ImportError:\r\n        pass\r\n    \r\n    \r\n    def apply_joint_pos(world_name, robot_name, joint_names, poses, duration=0):\r\n        wait_net_service('localhost', 11345)\r\n        time.sleep(1.0)\r\n        print(\"Net serviced detected. Proceeding\")\r\n    \r\n        @trollius.coroutine\r\n        def joint_pose_loop(world_name, robot_name, joint_name, pose, duration):\r\n            manager = yield From(pygazebo.connect())\r\n            print(\"connected\")\r\n            \r\n            \r\n            publisher = yield From(\r\n                manager.advertise('/gazebo/' + world_name + '/' + robot_name + '/joint_cmd',\r\n                                  'gazebo.msgs.JointCmd'))\r\n        \r\n            message = pygazebo.msg.joint_cmd_pb2.JointCmd()\r\n            message.name = robot_name + '::' + joint_name #format should be: name_of_robot + '::name_of_joint'\r\n            print(message.position.target)\r\n            message.position.target = pose\r\n            print(message.position.target)\r\n           # message.force = pose\r\n            try:\r\n                yield From(trollius.sleep(0.1))            \r\n                yield From(publisher.publish(message))  \r\n            except :\r\n                print(\"ugh oh\")\r\n                pass\r\n                \r\n            print(\"Connection closed\")\r\n            \r\n        def loop_in_thread(loop, tasks):\r\n            trollius.set_event_loop(loop)\r\n            loop.run_until_complete(trollius.wait(tasks))\r\n            \r\n        tasks = []\r\n        for joint_name, pose in zip(joint_names, poses):\r\n            tasks.append(trollius.Task(joint_pose_loop(world_name, robot_name, joint_name, pose, duration)))\r\n    \r\n        loop = trollius.get_event_loop()        \r\n        loop.run_until_complete(trollius.wait(tasks))\r\n            #TODO either integrate with PyGazebo or clean up, maybe with sub processes\r\n        #This is a Sandbox Nightmare. Poor Coding practices\r\n    \r\n    \r\n    def wait_net_service(server, port, timeout=None):\r\n        \"\"\" Wait for network service to appear \r\n            @param timeout: in seconds, if None or 0 wait forever\r\n            @return: True of False, if timeout is None may return only True or\r\n                     throw unhandled network exception\r\n        \"\"\"\r\n        import socket\r\n        import errno\r\n    \r\n        s = socket.socket()\r\n        if timeout:\r\n            from time import time as now\r\n            # time module is needed to calc timeout shared between two exceptions\r\n            end = now() + timeout\r\n    \r\n        while True:   \r\n            try:\r\n                if timeout:\r\n                    next_timeout = end - now()\r\n                    if next_timeout < 0:\r\n                        return False\r\n                    else:\r\n                        s.settimeout(next_timeout)\r\n                s.connect((server, port))\r\n            except socket.timeout as err:\r\n                # this exception occurs only if timeout is set\r\n                if timeout:\r\n                    return False\r\n          \r\n            except socket.error as err:\r\n                # catch timeout exception from underlying network library\r\n                # this one is different from socket.timeout\r\n                if type(err.args) != tuple or (err[0] != errno.ETIMEDOUT and err[0] != errno.ECONNREFUSED):\r\n                    raise err\r\n            else:\r\n                s.close()\r\n                return True\r\n    \r\n    world_name='world'\r\n    robot_name='default_body'\r\n    joint_names=['hingejoint0', 'hingejoint1', 'hingejoint2', 'hingejoint3', 'hingejoint4', 'hingejoint5', 'hingejoint6', 'hingejoint7', 'hingejoint8']\r\n    \r\n    apply_joint_pos(world_name, robot_name,['hingejoint1'], [9])\r\n    apply_joint_pos(world_name, robot_name,['hingejoint2'], [-9])\r\n    apply_joint_pos(world_name, robot_name,['hingejoint3'], [-7])\r\n    apply_joint_pos(world_name, robot_name,['hingejoint4','hingejoint5'], [-10,-10])\r\n    apply_joint_pos(world_name, robot_name,['hingejoint0'], [-21])\r\n    apply_joint_pos(world_name, robot_name,['hingejoint4','hingejoint5'], [-15,-15])\r\n    apply_joint_pos(world_name, robot_name,['hingejoint3'], [-12])"}}