Exemple #1
0
	def runInteraction(self, interaction, *args, **kw):
		"""
		Interact with the database and return the result.

		The 'interaction' is a callable object which will be executed
		in a thread using a pooled connection. It will be passed an
		L{Transaction} object as an argument (whose interface is
		identical to that of the database cursor for your DB-API
		module of choice), and its results will be returned as a
		Deferred. If running the method raises an exception, the
		transaction will be rolled back. If the method returns a
		value, the transaction will be committed.

		NOTE that the function you pass is *not* run in the main
		thread: you may have to worry about thread-safety in the
		function you pass to this if it tries to use non-local
		objects.

		@param interaction: a callable object whose first argument
			is an L{adbapi.Transaction}.

		@param *args: additional positional arguments to be passed
			to interaction

		@param **kw: keyword arguments to be passed to interaction

		@return: a Deferred which will fire the return value of
			'interaction(Transaction(...), *args, **kw)', or a Failure.
		"""
		from lib.twisted.internet import reactor
		return threads.deferToThreadPool(reactor, self.threadpool,
										 self._runInteraction,
										 interaction, *args, **kw)
Exemple #2
0
	def runWithConnection(self, func, *args, **kw):
		"""
		Execute a function with a database connection and return the result.

		@param func: A callable object of one argument which will be executed
			in a thread with a connection from the pool.  It will be passed as
			its first argument a L{Connection} instance (whose interface is
			mostly identical to that of a connection object for your DB-API
			module of choice), and its results will be returned as a Deferred.
			If the method raises an exception the transaction will be rolled
			back.  Otherwise, the transaction will be committed.  B{Note} that
			this function is B{not} run in the main thread: it must be
			threadsafe.

		@param *args: positional arguments to be passed to func

		@param **kw: keyword arguments to be passed to func

		@return: a Deferred which will fire the return value of
			C{func(Transaction(...), *args, **kw)}, or a Failure.
		"""
		from lib.twisted.internet import reactor
		return threads.deferToThreadPool(reactor, self.threadpool,
										 self._runWithConnection,
										 func, *args, **kw)
Exemple #3
0
    def runInteraction(self, interaction, *args, **kw):
        """
		Interact with the database and return the result.

		The 'interaction' is a callable object which will be executed
		in a thread using a pooled connection. It will be passed an
		L{Transaction} object as an argument (whose interface is
		identical to that of the database cursor for your DB-API
		module of choice), and its results will be returned as a
		Deferred. If running the method raises an exception, the
		transaction will be rolled back. If the method returns a
		value, the transaction will be committed.

		NOTE that the function you pass is *not* run in the main
		thread: you may have to worry about thread-safety in the
		function you pass to this if it tries to use non-local
		objects.

		@param interaction: a callable object whose first argument
			is an L{adbapi.Transaction}.

		@param *args: additional positional arguments to be passed
			to interaction

		@param **kw: keyword arguments to be passed to interaction

		@return: a Deferred which will fire the return value of
			'interaction(Transaction(...), *args, **kw)', or a Failure.
		"""
        from lib.twisted.internet import reactor
        return threads.deferToThreadPool(reactor, self.threadpool,
                                         self._runInteraction, interaction,
                                         *args, **kw)
Exemple #4
0
    def runWithConnection(self, func, *args, **kw):
        """
		Execute a function with a database connection and return the result.

		@param func: A callable object of one argument which will be executed
			in a thread with a connection from the pool.  It will be passed as
			its first argument a L{Connection} instance (whose interface is
			mostly identical to that of a connection object for your DB-API
			module of choice), and its results will be returned as a Deferred.
			If the method raises an exception the transaction will be rolled
			back.  Otherwise, the transaction will be committed.  B{Note} that
			this function is B{not} run in the main thread: it must be
			threadsafe.

		@param *args: positional arguments to be passed to func

		@param **kw: keyword arguments to be passed to func

		@return: a Deferred which will fire the return value of
			C{func(Transaction(...), *args, **kw)}, or a Failure.
		"""
        from lib.twisted.internet import reactor
        return threads.deferToThreadPool(reactor, self.threadpool,
                                         self._runWithConnection, func, *args,
                                         **kw)
Exemple #5
0
    def getHostByName(self, name, timeout=(1, 3, 11, 45)):
        """
		See L{lib.twisted.internet.interfaces.IResolverSimple.getHostByName}.

		Note that the elements of C{timeout} are summed and the result is used
		as a timeout for the lookup.  Any intermediate timeout or retry logic
		is left up to the platform via L{socket.gethostbyname}.
		"""
        if timeout:
            timeoutDelay = sum(timeout)
        else:
            timeoutDelay = 60
        userDeferred = defer.Deferred()
        lookupDeferred = threads.deferToThreadPool(
            self.reactor, self.reactor.getThreadPool(), socket.gethostbyname, name
        )
        cancelCall = self.reactor.callLater(timeoutDelay, self._cleanup, name, lookupDeferred)
        self._runningQueries[lookupDeferred] = (userDeferred, cancelCall)
        lookupDeferred.addBoth(self._checkTimeout, name, lookupDeferred)
        return userDeferred
Exemple #6
0
	def getHostByName(self, name, timeout = (1, 3, 11, 45)):
		"""
		See L{lib.twisted.internet.interfaces.IResolverSimple.getHostByName}.

		Note that the elements of C{timeout} are summed and the result is used
		as a timeout for the lookup.  Any intermediate timeout or retry logic
		is left up to the platform via L{socket.gethostbyname}.
		"""
		if timeout:
			timeoutDelay = sum(timeout)
		else:
			timeoutDelay = 60
		userDeferred = defer.Deferred()
		lookupDeferred = threads.deferToThreadPool(
			self.reactor, self.reactor.getThreadPool(),
			socket.gethostbyname, name)
		cancelCall = self.reactor.callLater(
			timeoutDelay, self._cleanup, name, lookupDeferred)
		self._runningQueries[lookupDeferred] = (userDeferred, cancelCall)
		lookupDeferred.addBoth(self._checkTimeout, name, lookupDeferred)
		return userDeferred