Qt4 Synchronous HTTP Request

Qt4 Synchronous HTTP Request | Erata.NET

 


Qt4 Synchronous HTTP Request

 

Problem

Creating a wrapper over QHttp that can perform GET and POST requests in a synchronous way.

 

Solution

In Qt4 QHttp can perform HTTP requests, but the API only allows asynchronous requests. This means that you need to specify a set of slots to handle the signals that can be emitted while the request if performed. Now don’t get me wrong, this is event driven programming and most of the times is the right way to go especially when writing a Qt based application.  The cases when event driven operations prove difficult to use are rare and usually can be programmed using events.

posted on 2012-04-19 23:08  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/04/19/2458586.html

"Synchronous"(同步)一般指的是计算机系统中的同步操作,即在进行某个操作时,必须等待前一个操作结束后才能开始下一个操作。下面是两个关于同步的例子: 1.在Python中使用同步机制实现多线程 Python的多线程模块中提供了Lock和RLock对象,可以使用它们来实现线程之间的同步操作。下面是一个简单的示例代码,它启动了两个线程,分别输出0~99和100~199的数字,并使用Lock对象来实现同步操作,避免两个线程同时输出数字。 ```python import threading class CountThread(threading.Thread): def __init__(self, lock, start, end): threading.Thread.__init__(self) self.lock = lock self.start = start self.end = end def run(self): for i in range(self.start, self.end): self.lock.acquire() print(i) self.lock.release() lock = threading.Lock() t1 = CountThread(lock, 0, 100) t2 = CountThread(lock, 100, 200) t1.start() t2.start() t1.join() t2.join() ``` 2.在JavaScript中使用同步机制实现阻塞操作 JavaScript是一种单线程语言,因此不能像Python那样使用多线程来实现同步操作。但是,JavaScript提供了一种类似于同步的机制,即使用Promise对象来实现异步操作。下面是一个使用Promise实现阻塞操作的示例代码,它在5秒后输出一条消息。 ```javascript function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function main() { console.log('Start'); await sleep(5000); console.log('End'); } main(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值