Hello!
I am trying to have an async network call occur in a custom omniverse extension nothing I try seems to work.
When I attempt to call a function, it causes the application to freeze upon load until data has been received, then the function will no longer work.
I have tried setting up a socket connection which unfortunately did not work
import omni.usd
import omni.kit
import omni.ui as ui
import omni.timeline
import omni.kit.viewport
import omni.kit.commands
from pxr import Gf, Sdf, Usd
import asyncio
import os
import threading
HOST = '127.0.0.1'
PORT = 65432
class Loader:
def __init__(self, ext_id):
asyncio.ensure_future(self.delayed_load())
ext_path = omni.kit.app.get_app().get_extension_manager().get_extension_path(ext_id)
self.window = None
self.timeline_interface = omni.timeline.get_timeline_interface()
self.stage_path = os.path.join(ext_path, "data/levels/warehouse_track.usda")
## Tried with asyncio library
# asyncio.run(self.startServer())
## Tried with Thread library
# threading.Thread(target=self.startServer).start()
async def delayed_load(self):
await omni.kit.app.get_app().next_update_async()
omni.usd.get_context().open_stage_with_callback(self.stage_path, self.on_stage_loaded)
async def startServer(self):
self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
max_attempts = 10
for i in range(max_attempts):
print("Attempting to connect to server: ", i)
try:
self.tcp_socket.bind((HOST, PORT))
break
except:
time.sleep(1)
if i == max_attempts:
print("failed to connect to server")
self.tcp_socket = None
return
self.tcp_socket.listen(1)
print("connecting...")
try:
self.client_socket, self.ip_port - self.tcp_socket.accept()
except Exception as e:
print(e)
return
print("CLient", self.ip_port, "connected")
self.running = True
self.should_continue = True
while self.should_continue:
data = self.client_socket.recv(1024)
if not data:
break
print(data)
print("client disconnected")
self.tcp_socket.close()
self.tcp_socket = None
self.running = False
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print("Ready for connection...")
conn, addr = s.accept()
try:
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
# print(data)
if not data:
break
conn.sendall(data)
return data
except KeyboardInterrupt:
print("Caught keyboard interrupt, exiting")
def start (self):
self.timeline_interface.play()
def shutdown(self):
self.window = None
I then tried doing the same connection but using RabbitMQ with the same result.
import omni.usd
import omni.kit
import omni.ui as ui
import omni.timeline
import omni.kit.viewport
import omni.kit.commands
from pxr import Gf, Sdf, Usd
import asyncio
import os
import threading
import pika
class Loader:
def __init__(self, ext_id):
asyncio.ensure_future(self.delayed_load())
ext_path = omni.kit.app.get_app().get_extension_manager().get_extension_path(ext_id)
self.window = None
self.timeline_interface = omni.timeline.get_timeline_interface()
self.stage_path = os.path.join(ext_path, "data/levels/warehouse_track.usda")
## Tried with asyncio library
# asyncio.run(self.startServer())
## Tried with Thread library
# threading.Thread(target=self.startServer).start()
async def delayed_load(self):
await omni.kit.app.get_app().next_update_async()
omni.usd.get_context().open_stage_with_callback(self.stage_path, self.on_stage_loaded)
async def startServer(self):
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='omniverse')
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
channel.basic_consume(queue='omniverse', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
def start (self):
self.timeline_interface.play()
def shutdown(self):
self.window = None
Is there a way, or any recommendation, to try and get a live stream of data into an Omniverse Application using Kit?
Thank you in advanced!