~rye/ubuntuone-client/unique-check-is-unique

« back to all changes in this revision

Viewing changes to bin/ubuntuone-preferences

  • Committer: Tarmac
  • Author(s): john.lenton at canonical
  • Date: 2010-09-13 18:51:04 UTC
  • mfrom: (689.1.4 fix-633241)
  • Revision ID: tarmac-20100913185104-uz8ob2lsti4056su
Do calls to libproxy in a separate process. (LP:633241)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# You should have received a copy of the GNU General Public License along
21
21
# with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 
23
 
from __future__ import division
 
23
from __future__ import division, with_statement
24
24
 
25
25
import pygtk
26
26
pygtk.require('2.0')
31
31
import time
32
32
import gettext
33
33
import subprocess
 
34
from multiprocessing import Pipe, Process
34
35
from threading import Thread
35
36
from oauth import oauth
36
37
 
37
38
from ubuntu_sso import DBUS_BUS_NAME, DBUS_IFACE_CRED_NAME, DBUS_CRED_PATH
38
39
from ubuntuone import clientdefs
39
40
from ubuntuone.syncdaemon.tools import SyncDaemonTool
40
 
from ubuntuone.api.restclient import RestClient
41
41
from ubuntuone.logger import (basic_formatter, LOGFOLDER,
42
42
                              CustomRotatingFileHandler)
43
43
 
117
117
    except DBusException, e:
118
118
        error_handler(e)
119
119
 
120
 
def do_rest_request(rest_client, url, method, callback):
121
 
    """Helper that handles the REST request.""" 
 
120
def really_do_rest_request(url, method, conn):
 
121
    """Second-order helper that does the REST request.
 
122
 
 
123
    Necessary because of libproxy's orneriness WRT threads: LP:633241.
 
124
    """
 
125
    from ubuntuone.api.restclient import RestClient
 
126
    logger.debug("really_do_rest_request (%s:%s)", method, url)
 
127
    rest_client = RestClient(url)
122
128
    result = rest_client.call(url, method, oauth_consumer, oauth_token)
123
 
 
124
 
    gtk.gdk.threads_enter()
125
 
    callback(result)
126
 
    gtk.gdk.threads_leave()
 
129
    logger.debug("got result for really_do_rest_request (%s:%s)", method, url)
 
130
    conn.send(result)
 
131
    logger.debug("end really_do_rest_request (%s:%s)", method, url)
 
132
 
 
133
def do_rest_request(proc, conn, callback):
 
134
    """Helper that handles the REST request."""
 
135
    pid = os.getpid()
 
136
    logger.debug("do_rest_request (%s)", pid)
 
137
    proc.join()
 
138
 
 
139
    result = conn.recv()
 
140
    logger.debug("got result for do_rest_request (%d)", pid)
 
141
    if callback is not None:
 
142
        with gtk.gdk.lock:
 
143
            callback(result)
 
144
    logger.debug("end do_rest_request (%d)", pid)
127
145
 
128
146
def make_rest_request(url=None, method='GET', callback=None):
129
147
    """Helper that makes an oauth-wrapped REST request."""
130
 
    rest_client = RestClient(url)
131
 
    Thread(target=do_rest_request, args=(rest_client, url, method, callback)).start()
 
148
    logger.debug("make_rest_request (%s:%s)", method, url)
 
149
    conn1, conn2 = Pipe(False)
 
150
    p = Process(target=really_do_rest_request, args=(url, method, conn2))
 
151
    p.start()
 
152
    logger.debug("make_rest_request (%s:%s) started %d", method, url, p.pid)
 
153
    Thread(target=do_rest_request, args=(p, conn1, callback)).start()
 
154
    logger.debug("end make_rest_request (%s:%s)", method, url)
132
155
 
133
156
class DevicesWidget(gtk.Table):
134
157
    """