Advertisement
caffeinatedmike

Asyncio.Queue() Attempt at Single-File One-Lane Queue

Nov 9th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import asyncio
  2.  
  3. async def produce_output(queue, commands):
  4.     for command in commands:
  5.         #execute the adb command
  6.         if 'keypress' in command:
  7.             #command contains 'input keypress ENTER'
  8.             adb.Shell(command)
  9.             #mark the task done because there's nothing to process
  10.             queue.task_done()
  11.         else:
  12.             #command contains 'dumpsys audio'
  13.             output = adb.Shell(command)
  14.             #put result in queue
  15.             await queue.put(output)
  16.    
  17. async def process_adb(queue):
  18.     while True:
  19.         output = await queue.get()
  20.         #return output (somehow?)
  21.         queue.task_done()
  22.  
  23.  
  24. async def update():
  25.     adb_queue = asyncio.Queue()
  26.     asyncio.create_task(produce_output(adb_queue,
  27.         [self._screen_on,
  28.          self.current_app,
  29.          self._wake_lock,
  30.          self._dump('audio')]))
  31.     #Not sure how to proceed
  32.  
  33.     if not self._adb:
  34.         self.state = STATE_UNKNOWN
  35.         self.app_id = None
  36.     # Check if device is off.
  37.     # Fetching result of first item in the queue - self._screen_on
  38.     elif not await adb_queue.get():
  39.         self.state = STATE_OFF
  40.         self.app_id = None
  41.     else:
  42.         # Fetching result of second item in the queue - self.current_app
  43.         self.app_id = await adb_queue.get()
  44.        
  45.         # Fetching result of third item in the queue - self._wake_lock
  46.         if await adb_queue.get():
  47.             self.state = STATE_PLAYING
  48.         elif self.app_id not in (self.package_launcher, self.package_settings):
  49.             # Check if state was playing on last update
  50.             if self.state == STATE_PLAYING:
  51.                 self.state = STATE_PAUSED
  52.             elif self.state != STATE_PAUSED:
  53.                 self.state = STATE_IDLE
  54.         else:
  55.             # We're on either the launcher or in settings
  56.             self.state = STATE_ON
  57.        
  58.         # Get information from the audio status.
  59.         # Fetching result of fourth item in the queue - self._dump('audio')
  60.         audio_output = await adb_queue.get()
  61.         stream_block = re.findall(BLOCK_REGEX, audio_output,
  62.                                   re.DOTALL | re.MULTILINE)[0]
  63.         self.muted = re.findall(MUTED_REGEX, stream_block,
  64.                                 re.DOTALL | re.MULTILINE)[0] == 'true'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement