K
K
KPEBETKA2014-01-14 14:07:59
Python
KPEBETKA, 2014-01-14 14:07:59

How can DBusGMainLoop be parallelized?

It is necessary to scan any removable media with ClamAV. And it seems to have written everything, but the scans are sequential, which slows down the process. I can't figure out how to parallelize correctly.
At the moment the script looks like this:

#!/usr/bin/env python

import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop
from threading import Thread
import subprocess
import os

DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()

def extract_string(data):
  s = None
  if len(data):
    s = ''
    for d in data:
      s += d
  return s

def device_added_callback(device):
  device_obj = bus.get_object("org.freedesktop.UDisks", device)
  device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
  proplist =[]
  proplist.append(device_props.GetAll('org.freedesktop.UDisks.Device'))
  
  for device_props in proplist:
    if device_props['DeviceIsMounted']:
      d =  'Device ' + device_props['DriveVendor'] + ' ' +device_props['DriveModel'] + ' "'  + device_props['IdLabel'] + '" ' + str(round(float(device_props['PartitionSize'])/(1024*1024*1024), 2)) + 'GB was added'
      s = str(extract_string(device_props['DeviceMountPaths']))
      print d + '\n' + s
      
      ps = Thread(target=clam_scan(s), args=())
      ps.daemon = True
      ps.start()

def clam_scan(s):
  print "start scan"
  proc = subprocess.Popen(['clamscan -ir "' + str(s) +'"'], stdout=subprocess.PIPE, shell=True)
  (out, err) = proc.communicate()
  print "finish scan"

  for i in out.split('\n'):
    for j in ['Scanned directories:', 'Scanned files:', 'Infected files:', 'Data scanned:', 'Data read:', 'Time:']:
      if i.find(j) > -1:
        print i

def main():
  mainloop = gobject.MainLoop()
  udisks_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
  udisks = dbus.Interface(udisks_obj, 'org.freedesktop.UDisks')
  devices = udisks.get_dbus_method('EnumerateDevices')()
  udisks.connect_to_signal('DeviceChanged', device_added_callback)
  mainloop.run()

if __name__ == '__main__':

  main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
avalak, 2014-01-14
@KPEBETKA

After imports gobject.threads_init()and
why

def extract_string(data):
    s = None
    if len(data):
        s = ''
        for d in data:
            s += d
    return s

when you can just ''.join(data)
well, in general, it would be nice to clean the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question