Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';trollius让这个循环异步运行吗?_Python_Python 2.7_Loops_Asynchronous_Concurrency - Fatal编程技术网

Python 为什么不是';trollius让这个循环异步运行吗?

Python 为什么不是';trollius让这个循环异步运行吗?,python,python-2.7,loops,asynchronous,concurrency,Python,Python 2.7,Loops,Asynchronous,Concurrency,我试图在Pygazebo中编写一些简单的循环来控制对象,但遗憾的是,它只调用了一次该方法,然后循环似乎被阻塞了 # -*- coding: utf-8 -*- """ Created on Thu Jul 2 12:52:50 2015 @author: skylion """ import trollius #NOTE: Trollius requires protobuffer from Google from trollius import From import pygazebo

我试图在Pygazebo中编写一些简单的循环来控制对象,但遗憾的是,它只调用了一次该方法,然后循环似乎被阻塞了

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  2 12:52:50 2015

@author: skylion
"""

import trollius #NOTE: Trollius requires protobuffer from Google
from trollius import From

import pygazebo
import pygazebo.msg.joint_cmd_pb2
import time   

def apply_joint_force(world_name, robot_name, joint_name, force, duration=-1):


    @trollius.coroutine 
    def joint_force_loop():
        manager = yield From(pygazebo.connect())
        print("connected")


        publisher = yield From(
            manager.advertise('/gazebo/' + world_name + '/' + robot_name + '/joint_cmd',
                              'gazebo.msgs.JointCmd'))

        message = pygazebo.msg.joint_cmd_pb2.JointCmd()
        message.name = robot_name + '::' + joint_name #format should be: name_of_robot + '::name_of_joint'
        message.force = force


        #t_end = time.time() + duration # The time that you want the controller to stop
        while True: #time.time() < t_end or duration == -1:
            try:
                yield From(publisher.publish(message))
                yield From(trollius.sleep(1.0))
            except:
                pass
             #Nothing   
        print("Connection closed")

    wait_net_service('localhost',11345)


    loop = trollius.new_event_loop()
    loop.run_until_complete(joint_force_loop())
    raise     


def wait_net_service(server, port, timeout=None):
    """ Wait for network service to appear 
        @param timeout: in seconds, if None or 0 wait forever
        @return: True of False, if timeout is None may return only True or
                 throw unhandled network exception
    """
    import socket
    import errno

    s = socket.socket()
    if timeout:
        from time import time as now
        # time module is needed to calc timeout shared between two exceptions
        end = now() + timeout

    while True:
        try:
            if timeout:
                next_timeout = end - now()
                if next_timeout < 0:
                    return False
                else:
                    s.settimeout(next_timeout)
            s.connect((server, port))
            time.sleep(1)
        except socket.timeout, err:
            # this exception occurs only if timeout is set
            if timeout:
                return False

        except socket.error, err:
            # catch timeout exception from underlying network library
            # this one is different from socket.timeout
            if type(err.args) != tuple or (err[0] != errno.ETIMEDOUT and err[0] != errno.ECONNREFUSED):
                raise err
        else:
            s.close()
            return True

你知道为什么它一直阻塞线程吗?我应该使用不同的方法来解决这个问题吗?任何帮助都将不胜感激

因此,答案非常简单。在启动对象之前,您必须将要作为列表运行的多个Trollius.Tasks排队,并将其与Trollius.wait()组合以实现此目的。要确保线程是非阻塞的,请使用以下命令

以下是我目前的代码:

tasks = []
for joint_name in joint_names:
    tasks.append(trollius.Task(joint_force_loop(world_name, robot_name, joint_name, force, duration))
loop = trollius.get_event_loop()
loop.run_until_complete(trollius.wait(tasks))

我认为您需要将
wait\u net\u服务('localhost',11345)
更改为
yield from wait\u net\u服务('localhost',11345)
我甚至在添加该方法之前就遇到了这个问题。所以这不是问题所在。
tasks = []
for joint_name in joint_names:
    tasks.append(trollius.Task(joint_force_loop(world_name, robot_name, joint_name, force, duration))
loop = trollius.get_event_loop()
loop.run_until_complete(trollius.wait(tasks))