Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MASTERING PYCHARM

You're reading from   MASTERING PYCHARM Use PyCharm with fluid efficiency to write idiomatic python code

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781783551316
Length 232 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Nafiul Islam Nafiul Islam
Author Profile Icon Nafiul Islam
Nafiul Islam
Arrow right icon
View More author details
Toc

Dealing with threads and processes


PyCharm has very good support for dealing with threads. Just like how we can change frames, we can also change threads if we so wish (that is, if we have more than one thread to begin with). Let's take the example of a simple downloader script called downloader.py:

# encoding=utf-8
from threading import Thread

import requests


def download(url):
    response = requests.get(url)
    if response.status_code == 200:
        print "Success -> {:<75} | Length -> {}".format(response.url, len(response.content))
    else:
        print "Failure -> {:>75}".format(response.url)


if __name__ == '__main__':
    urls = "http://www.google.com http://www.bing.com http://www.yahoo.com http://news.ycombinator.com".split()

    for u in urls:
        Thread(target=download, args=(u,)).start()

Tip

To run this code, you'll need to install requests though pip install requests in the command line.

This is a simple script that sends a get request to a url (there...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime