RuntimeError: Explicitly using 'twisted' already

#1

I am trying to use twisted,kivy and autobahn in my application. I am getting the following error.

Error:


Abhishek@ABHISHEK ~/Dropbox/abhishek_bhatia/notepad_app (master)

$ python __main__.py

[INFO              ] [Logger      ] Record log in c:/Users/sony\.kivy\logs\kivy_16-04-26_5.txt

[INFO              ] [Kivy        ] v1.9.1

[INFO              ] [Python      ] v2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)]

[INFO              ] [Factory     ] 179 symbols loaded

[INFO              ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2, img_pil (img_ffpyplayer ignored)

[INFO              ] [Text        ] Provider: sdl2

Traceback (most recent call last):

File "__main__.py", line 19, in <module>

from canvas.canvas_widget import CanvasWidget,RadioButton

File "c:\Users\sony\Dropbox\abhishek_bhatia\notepad_app\canvas\canvas_widget.py", line 23, in <module>

from network_comm.audio_client_tcp import AudioClientFactory

File "c:\Users\sony\Dropbox\abhishek_bhatia\notepad_app\canvas\network_comm\audio_client_tcp.py", line 3, in <module>

from autobahn.asyncio.websocket import WebSocketServerFactory

File "c:\Users\sony\virtualenvs\noteapp\lib\site-packages\autobahn\asyncio\__init__.py", line 36, in <module>

from autobahn.asyncio.websocket import \

File "c:\Users\sony\virtualenvs\noteapp\lib\site-packages\autobahn\asyncio\websocket.py", line 32, in <module>

txaio.use_asyncio()

File "c:\Users\sony\virtualenvs\noteapp\lib\site-packages\txaio\__init__.py", line 117, in use_asyncio

raise RuntimeError("Explicitly using '{}' already".format(_explicit_framework))

RuntimeError: Explicitly using 'twisted' already

(noteapp)

Code:


import txaio

txaio.use_twisted()

#from twisted.internet.protocol import ClientFactory

import pdb,json

from autobahn.asyncio.websocket import WebSocketServerFactory

#from twisted.protocols.basic import NetstringReceiver

from twisted.internet.defer import inlineCallbacks

from twisted.internet.threads import deferToThread

from twisted.internet.defer import DeferredQueue

# audio imports

import pyaudio,wave,threading

from Queue import Queue

class AudioClient(NetstringReceiver):

	#audio parameters

	CHUNK = 1024

	FORMAT = pyaudio.paInt16

	CHANNELS = 1

## initialisation

	def __init__(self,factory,canvas_obj):

		'''initiates with factory and canvas object

		Reasons:

			factory:

			canvas object:

		'''

		self.factory = factory

		self.canvas_obj=canvas_obj

	def connectionMade(self):

		'''sends the username of the client to register the protocol at remore server with corresponding

		address'''

		print "Connected to audio remote server"

		self.sendString(json.dumps({'username':self.factory.username}))

		# start recording and sending audio

		if self.factory.canvas_obj.allow_all or self.factory.canvas_obj.client_state=='server' \

			or self.factory.canvas_obj.client_audio_send:

			self.start_audio_stream()

		# start receiving and playing audio

		self.receive_audio_stream()

## record and send

	def start_audio_stream(self):

		'''creates an audio stream for sending data

		A callback is defined on send_audio_data() to send the stream

		'''

		# create PyAudio Object

		self.send_stream_Pyobj=pyaudio.PyAudio()

		self.send_rate=int(self.send_stream_Pyobj.get_device_info_by_index(0)['defaultSampleRate']/4)

		# create stream

		self.send_stream=self.send_stream_Pyobj.open(format=self.FORMAT,channels=self.CHANNELS,

			rate=self.send_rate,input=True,frames_per_buffer=self.CHUNK,output=False)

		# start the stream

		self.send_stream.start_stream()

		self.send_audio_data_thread = threading.Thread(name='send_audio_data'\

			,target=self.send_audio_data)

		self.send_audio_data_thread.daemon=True

		self.send_audio_data_thread.start()

	@inlineCallbacks

	def send_audio_data(self):

		'''sends the audio chunk to remote server as string'''

		while True:

			audio_data = yield self.send_stream.read(self.CHUNK)

			self.sendString(audio_data)

## receive and play

	def receive_audio_stream(self):

		''' 1. creates an audio stream for receiving data

			2. creates an audio queue

			3. initiates a thread for playing the received audio

		'''

		# create PyAudio Object

		self.receive_stream_Pyobj=pyaudio.PyAudio()

		self.receive_rate=int(self.receive_stream_Pyobj.get_device_info_by_index(0)['defaultSampleRate']/4)

		# create stream

		self.stream_receive = self.receive_stream_Pyobj.open(format=self.FORMAT,channels=self.CHANNELS,

				rate=self.receive_rate,output=True,input=False)

		# start the receiving stream

		self.stream_receive.start_stream()

		# create a queue which gets filled in from data received

		self.recv_audio_queue=Queue() # queue that stores the audio received

		# create and start the thread for playing the received audio

		self.play_audio_received_thread = threading.Thread(name='play_audio_received'\

			,target=self.play_audio_received)

		self.play_audio_received_thread.daemon=True

		self.play_audio_received_thread.start()

	def stringReceived(self,data):

		'''appends the audio string received to queue(recv_audio_queue) which is played by

		play_audio_received thread'''

		self.recv_audio_queue.put(data)

	def play_audio_received(self):

		while True:

			self.stream_receive.write(self.recv_audio_queue.get())

			#self.recv_audio_queue.task_done()

## close streams, terminate pyaudio objects and stop threads

	def connectionLost(self, reason):

		# close send audio if exists

		try:

			self.send_stream.stop_stream()

			self.send_stream.close()

			self.send_stream_Pyobj.terminate()

		except:

			print "Send audio stream and object doesn't exist"

		# close receive audio stream and thread

		try:

			self.stream_receive.stop_stream()

			self.stream_receive.close()

			self.receive_stream_Pyobj.terminate()

			self.play_audio_received_thread.stopped=True

		except:

			print "Receive audio stream and object doesn't exist"

class AudioClientFactory(ClientFactory):

	def __init__(self, canvas_obj,username):

		self.canvas_obj = canvas_obj

		self.username=username

	def buildProtocol(self, addr):

		self.protocol=AudioClient(self,self.canvas_obj)

		return self.protocol

if __name__ == '__main__':

	from twisted.internet import reactor

	reactor.connectTCP("127.0.0.1",3036,AudioClientFactory('junkcanvas_obj','something'))

	reactor.run()

0 Likes