“The Deuce” is gritty — literally. Set in New York City in 1971, the show has a fanatical attention to period detail that is the hallmark of co-creator and co-showrunner David Simon. And the Deuce itself — the section of 42nd Street from Sixth to Eighth Avenues — is, in 1971, a crowded, seedy stretch of adult movie houses, peep shows, dive bars and prostitutes. A layer of grime, so palpable it’s sticky, coats the sidewalks and the cars. When vagrants aren’t peeing in phone booths, less discerning johns and their hired girls are using them for a quick blow job. The whores’ high heels seem not an affectation but a necessity, to maintain at least the illusion of distance from the filth, metaphorical and literal, of the sidewalk. In the pilot, when one of the girls walks home barefoot after a long night in heels, it’s so abject it’s physically repulsive: That is how believable the atmosphere is.

It’s natural to compare “The Deuce” to 2002’s “The Wire,” Simon’s most famous project, which not only was a great cop show but also signified a paradigm shift for premium cable dramas. “The Deuce” is a worthy heir to the sprawling sociopolitics of “The Wire.” Indeed, it’s the closest thing to “The Wire” that Simon has produced in the 15 years since that show debuted — an immersive drama of life in a city, centered on the bleeding edge where crime meets culture. “The Wire” chased the increasingly sophisticated drug trade. “The Deuce,” from Simon and co-creator and co-showrunner George Pelecanos, who worked with him on “The Wire” and “Treme,” follows the barely legal world of sex work, from the streetwalkers and the coin-op peep shows to the filmmakers and mobsters behind the hustling of erotic films to underground audiences.

The problem with “The Deuce” — and this is a familiar problem for viewers who struggled to warm up to the dense worlds of “Treme” and “The Wire” — is that while the environment and themes are seductive, the plot is either irrelevant or seemingly nonexistent. This usually doesn’t feel like a problem. Simon and Pelecanos have a knack for making every detail significant, and by examining the lives of sex workers, pimps, cops and mobsters with equal interest, “The Deuce” tells a wide-ranging story of exploitation, sexuality and gender in a not-so-distant historical era. But it is subtle: It’s four to six episodes in before the arcs become clear. For a show ostensibly about pornography, only one character really explores that world. And even by the end of the first season, it never quite becomes clear why James Franco is playing twins.

Franco is Vinnie Martino, a double-shifting barman, and Frankie Martino, an inveterate gambler perpetually in debt to the mob. The reason he plays both is that apparently, both existed — and in all likelihood, the opportunity to take on two characters appealed to Franco, who inhabits them with a lot of verve. But it’s ironic that in a show so devoted to realism, the fact of mobbed-up twins falling backward into running whores and peddling pornography strikes the most false note.

To Franco’s credit, each Martino is a credibly unique performance, and Vinnie (whom we see much more of) is sympathetic, as much for his failings as for his acts of minor heroism. Vinnie kind of floats through life, but he’s more sensitive than Frankie, and as a result he sees the violence underpinning the trade. It paralyzes him to the point of inaction. In an arresting scene at the end of the pilot, he watches two of his regulars, a pimp and a prostitute, as one cuts the other with a switchblade. He is struck with something between pity and fear but does nothing.

Without fully being able to unpack it, much of “The Deuce’s” interest is in how fear, violence and eroticism run together, both for the johns coming to the Deuce and in the sex workers’ everyday lives. The show is at its absolute best, and most gutting, when it explores the complexity of the relationship between pimp and prostitute — who move between business and abuse and something like love with staggering fluidity.

“The Deuce” implies that pornography — the presentation of an erotic image to an assumed male audience — is an extension of the basic artifice of sex work. It looks at the women from behind the mask they present to their customers, but the mask is part of the story too. That’s why the attempt made by Candy (Maggie Gyllenhaal) to leave prostitution and move into pornography is the most compelling through line of the piece: From in front of and eventually behind the camera, Candy can use her awareness of the male gaze to make a product of herself and the other women on-screen. (It is satisfying, too, that in a show partly directed by Michelle MacLaren and other women directors, Candy is essentially making the case for diversity behind the camera.) Gyllenhaal has always had remarkable screen presence; as Candy, she has found a role that brings her considerable powers to bear.

“The Deuce” pulls extraordinary performances out of all of its actors: Gary Carr is perfectly snakelike as pimp C.C., and fellow regular Dominique Fishback, as the thoughtful prostitute Darlene, rapidly becomes the show’s emotional core. Even small roles, like Vinnie’s wife, Andrea (Zoe Kazan), and local diner owner Leon (Anwan Glover) are rich players. That’s the thing about Simon: When the story doesn’t quite make sense, the characters still do. All of them, no matter how marginal, feel like human beings at the center of their own life story.

A lot of storytelling is crammed into “The Deuce” — the regular and recurring cast fields nearly 40 characters, and it takes a minute to match rhythm with the cadence of each of their lives. But once you do, it’s a fascinating world: period but not nostalgic, lived in but not superficial. Maybe it has too many moving parts, but all the moving parts are a joy to look at.